class Dry::Resource

Constants

ATTR_RELATION_PATTERN
ATTR_TYPE_TO_FORM_FIELD
DEFAULT_OPTIONS

Attributes

model[R]

Public Class Methods

new(model, options = {}) click to toggle source
# File lib/dry/resource.rb, line 26
def initialize model, options = {}
  @model    = model
  @options  = DEFAULT_OPTIONS.merge options
end

Public Instance Methods

attrs_options() click to toggle source
# File lib/dry/resource.rb, line 71
def attrs_options
  @options[:attrs_options]
end
attrs_read() click to toggle source
# File lib/dry/resource.rb, line 63
def attrs_read
  @options[:attrs_read]
end
attrs_write() click to toggle source
# File lib/dry/resource.rb, line 67
def attrs_write
  @options[:attrs_write]
end
destroy?() click to toggle source
# File lib/dry/resource.rb, line 59
def destroy?
  has_route_for? :destroy
end
each_form_fields() { |e, reflections.klass| ... } click to toggle source
# File lib/dry/resource.rb, line 75
def each_form_fields
  attrs_write.each do |e|
    if e =~ ATTR_RELATION_PATTERN
      yield e,
        @model.reflections[e.to_s.sub ATTR_RELATION_PATTERN, ''].klass
    else
      yield e, form_field_for(e)
    end
  end
end
edit?() click to toggle source
# File lib/dry/resource.rb, line 55
def edit?
  has_route_for? :edit
end
new?() click to toggle source
# File lib/dry/resource.rb, line 51
def new?
  has_route_for? :new
end
plural_name() click to toggle source
# File lib/dry/resource.rb, line 35
def plural_name
  model_name.plural
end
relations() click to toggle source
# File lib/dry/resource.rb, line 43
def relations
  @options[:relations]
end
relations?() click to toggle source
# File lib/dry/resource.rb, line 39
def relations?
  @options[:relations].any?
end
show?() click to toggle source
# File lib/dry/resource.rb, line 47
def show?
  has_route_for? :show
end
singular_name() click to toggle source
# File lib/dry/resource.rb, line 31
def singular_name
  model_name.singular
end

Private Instance Methods

form_field_for(attr) click to toggle source
# File lib/dry/resource.rb, line 92
def form_field_for attr
  if type = attrs_options[attr]
    type
  else
    ATTR_TYPE_TO_FORM_FIELD[@model.columns_hash[attr.to_s].type]
  end
end
has_route_for?(action) click to toggle source
# File lib/dry/resource.rb, line 88
def has_route_for? action
  @options[:routes].include? action
end