Class: Granite::Form::Model::Attributes::Reflections::Represents

Inherits:
Attribute
  • Object
show all
Defined in:
lib/granite/form/model/attributes/reflections/represents.rb,
lib/granite/form/model/attributes/reflections/represents/build_type_definition.rb

Defined Under Namespace

Classes: BuildTypeDefinition

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attribute

#defaultizer, generate_methods, #normalizers

Methods inherited from Base

#build_attribute, #enum, generate_methods, #keys, #readonly

Constructor Details

#initialize(name, options) ⇒ Represents

Returns a new instance of Represents.

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 27

def initialize(name, options)
  super
  raise ArgumentError, "Undefined reference for `#{name}`" if reference.blank?
end

Class Method Details

.attribute_classObject



7
8
9
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 7

def self.attribute_class
  Attributes::Represents
end

.build(target, generated_methods, name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 11

def self.build(target, generated_methods, name, *args, &block)
  options = args.extract_options!

  reference = target.reflect_on_association(options[:of]) if target.respond_to?(:reflect_on_association)
  reference ||= target.reflect_on_attribute(options[:of]) if target.respond_to?(:reflect_on_attribute)
  options[:of] = reference.name if reference

  if options.fetch(:validate_reference, true)
    validates_nested = target.respond_to?(:validates_nested) && !target.validates_nested?(options[:of])
    target.validates_nested(options[:of]) if validates_nested
    target.validates_presence_of(options[:of]) unless target.validates_presence?(options[:of])
  end

  super(target, generated_methods, name, *args, options, &block)
end

Instance Method Details

#columnObject



40
41
42
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 40

def column
  @column ||= options[:column].presence.try(:to_s) || name
end

#inspect_reflectionObject



56
57
58
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 56

def inspect_reflection
  "#{name}: (represents)"
end

#readerObject



44
45
46
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 44

def reader
  @reader ||= options[:reader].presence.try(:to_s) || column
end

#reader_before_type_castObject



48
49
50
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 48

def reader_before_type_cast
  @reader_before_type_cast ||= "#{reader}_before_type_cast"
end

#referenceObject



36
37
38
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 36

def reference
  @reference ||= options[:of].to_s
end

#typeObject



32
33
34
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 32

def type
  Object
end

#writerObject



52
53
54
# File 'lib/granite/form/model/attributes/reflections/represents.rb', line 52

def writer
  @writer ||= "#{options[:writer].presence || column}="
end