Class: Granite::Form::Model::Attributes::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/granite/form/model/attributes/base.rb

Direct Known Subclasses

Attribute, ReferenceOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_definition) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/granite/form/model/attributes/base.rb', line 11

def initialize(type_definition)
  @type_definition = type_definition
  @origin = :default
end

Instance Attribute Details

#type_definitionObject (readonly)

Returns the value of attribute type_definition.



6
7
8
# File 'lib/granite/form/model/attributes/base.rb', line 6

def type_definition
  @type_definition
end

Instance Method Details

#came_from_default?Boolean

Returns:



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

def came_from_default?
  @origin == :default
end

#came_from_user?Boolean

Returns:



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

def came_from_user?
  @origin == :user
end

#inspect_attributeObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/granite/form/model/attributes/base.rb', line 60

def inspect_attribute
  value = case read
          when Date, Time, DateTime
            %("#{read.to_formatted_s(:db)}")
          else
            inspection = read.inspect
            inspection.size > 100 ? inspection.truncate(50) : inspection
          end
  "#{name}: #{value}"
end

#polluteObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/granite/form/model/attributes/base.rb', line 71

def pollute
  pollute = owner.class.dirty? && !owner.send(:attribute_changed?, name)

  if pollute
    previous_value = owner.__send__(name)
    owner.send("#{name}_will_change!")

    result = yield

    owner.__send__(:clear_attribute_changes, [name]) if owner.__send__(name) == previous_value

    if previous_value != read || (
      read.respond_to?(:changed?) &&
        read.changed?
    )
      owner.send(:set_attribute_was, name, previous_value)
    end
    result
  else
    yield
  end
end

#queryObject



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

def query
  !(read.respond_to?(:zero?) ? read.zero? : read.blank?)
end

#readObject



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

def read
  @value_cache
end

#read_before_type_castObject



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

def read_before_type_cast
  @value_cache
end

#readonly?Boolean

Returns:



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

def readonly?
  !!owner.evaluate(readonly)
end

#resetObject



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

def reset
  remove_variable(:value, :value_before_type_cast)
end

#value_present?Boolean

Returns:



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

def value_present?
  !read.nil? && !(read.respond_to?(:empty?) && read.empty?)
end

#write(value) ⇒ Object



22
23
24
25
26
# File 'lib/granite/form/model/attributes/base.rb', line 22

def write(value)
  return if readonly?

  write_value value
end

#write_value(value, origin: :user) ⇒ Object



16
17
18
19
20
# File 'lib/granite/form/model/attributes/base.rb', line 16

def write_value(value, origin: :user)
  reset
  @origin = origin
  @value_cache = value
end