Module: Attributable::ClassMethods

Defined in:
app/models/attributable.rb

Overview

Class methods for attribute configuration

Instance Method Summary collapse

Instance Method Details

#association(name, instance_method, options = {}) ⇒ Object

Defines an association with the name.


111
112
113
114
115
# File 'app/models/attributable.rb', line 111

def association(name, instance_method, options = {})
  association = Association.new(self, name, instance_method, options)
  association.configure(self)
  self.association_details += [association]
end

#attribute_details_for(attribute_name) ⇒ Attributable::Attribute

Looks up the Attribute in a attribute_details Array


136
137
138
# File 'app/models/attributable.rb', line 136

def attribute_details_for(attribute_name)
  attribute_details.detect { |d| d.name.to_sym == attribute_name.to_sym } || raise(StandardError, "Unknown attribute #{attribute_name}")
end

#attribute_namesArray<String>


127
128
129
# File 'app/models/attributable.rb', line 127

def attribute_names
  attribute_details.map(&:name)
end

#custom_attribute(name, options = {}, override_previous = false) ⇒ void

Note:

Heavy on meta-programming here. There behaviour could also be tidied up and simplified significantly.

This method returns an undefined value.

Define a custom attribute with the provided name. Will automatically generate:

- Validations
- Form helpers
- Accessioning tie-ins
- Convert blank attributes to nil

Options Hash (options):

  • :default (Object)

    A default value for the option. (Not a proc/lambda)

  • :required (Boolean) — default: false

    Whether the option is required or not

  • :validator (Boolean) — default: false

    Set to true to defer validation to the #validator_for method

  • :integer (Boolean) — default: false

    The attribute should be an integer

  • :positive_float (Boolean) — default: false

    The attribute should be a float, greater than 0

  • :boolean (Boolean) — default: false

    The attribute should be true or false. WARNING! Currently just tests presence of the key, not actual value. Thus false=true.

  • :in (Array) — default: nil

    The attribute is a selection that must be included in the array.

  • :selection (Boolean) — default: false

    The attribute is a selection generated dynamically from #validator_for

  • :minimum (Numeric) — default: 0

    The minimum value for an integer. WARNING! Inconsistently implemented for floats

  • :with (Regexp) — default: nil

    Regexp for validating the attribute

  • :if (Symbol) — default: nil

    Passed through to the rails validator and will also switch persistence of the attribute based on the condition.

  • :on (Symbol) — default: nil

    Passed through to the rails validator. (eg. on: :create)

  • :save_blank (Boolean) — default: false

    set to true to disabling setting blank attributes to nil. (UNUSED)


98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/attributable.rb', line 98

def custom_attribute(name, options = {}, override_previous = false)
  attribute = Attribute.new(self, name, options)
  attribute.configure(self)

  if override_previous
    self.attribute_details = attribute_details.reject { |a| a.name == name }
    self.attribute_details += [attribute]
  elsif self.attribute_details.detect { |a| a.name == name }.nil?
    self.attribute_details += [attribute]
  end
end

#defaultsHash<String,Object>

Returns a hash of default attribute values


120
121
122
123
124
# File 'app/models/attributable.rb', line 120

def defaults
  @defaults ||= attribute_details.each_with_object({}) do |attribute, hash|
    hash[attribute.name] = attribute.default
  end
end