class Frigate::Form::Association

Uses to define a form association with properties as Frigate::Form::Property

Attributes

associations[R]
name[R]
opts[R]
parent[R]
properties[R]
root[R]

Public Class Methods

associations() click to toggle source

gets just options for defined associations @example associations hash

{ some_name: { block: <Proc.new instance> }
# File lib/frigate/form/association.rb, line 33
def associations
        @associations.deep_dup || {}
end
has_one(name, options={}, &block) click to toggle source

defines association (kinda property with properties) for association @param [Symbol] name @param [Hash] options @param [Proc] block

# File lib/frigate/form/association.rb, line 18
def has_one(name, options={}, &block)
        @associations ||= {}
        @associations[name.to_sym] = options.merge({ block: block })
end
new(name, root, parent, opts = {}) click to toggle source

@param [Symbol] name @param [Hash] opts

# File lib/frigate/form/association.rb, line 41
def initialize(name, root, parent, opts = {})
        @name, @root, @parent, @opts = name, root, parent, opts
        @properties, @associations = [], []

        exec_opts_block
        process_properties
        process_associations
end
properties() click to toggle source

gets just options for defined properties @example properties hash

{ skype: { validates: { presence: true }, another_option: another_option_hash } }
# File lib/frigate/form/association.rb, line 26
def properties
        @properties.deep_dup || {}
end
property(name, options={}) click to toggle source

defines property for association @param [Symbol] name @param [Hash] options

# File lib/frigate/form/association.rb, line 9
def property(name, options={})
        @properties ||= {}
        @properties[name.to_sym] = options
end

Public Instance Methods

errors() click to toggle source

gets errors of association @return [ActiveModel::Errors]

# File lib/frigate/form/association.rb, line 52
def errors
        @errors ||= ActiveModel::Errors.new(self)
end
valid?() click to toggle source

checks validness of association properties

# File lib/frigate/form/association.rb, line 57
def valid?
        errors.messages.empty?
end
validate() click to toggle source

validates association properties

# File lib/frigate/form/association.rb, line 62
def validate
        properties.each { |_prop| errors.add(_prop.name, _prop.errors[:value]) unless _prop.valid? }
end

Private Instance Methods

exec_opts_block() click to toggle source

exec_opts_block

# File lib/frigate/form/association.rb, line 69
def exec_opts_block
        singleton_class.instance_exec &@opts.delete(:block)
end
process_associations() click to toggle source

initializes declared associations

# File lib/frigate/form/association.rb, line 74
def process_associations
        singleton_class.associations.each do |_assoc_name, _assoc_options|
                _assoc = Association.new(_assoc_name, root, self, _assoc_options)
                @associations << _assoc
                define_singleton_method(_assoc.name) { _assoc }
        end
end
process_properties() click to toggle source

initializes declared properties

# File lib/frigate/form/association.rb, line 83
def process_properties
        singleton_class.properties.each do |_prop_name, _prop_options|
                _prop = Property.new(_prop_name, root, self, _prop_options)
                @properties << _prop
                define_singleton_method(_prop.name) { _prop }
        end
end