Class: Frigate::Form::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/frigate/form/association.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, root, parent, opts = {}) ⇒ Association

Returns a new instance of Association.

Parameters:

  • name (Symbol)
  • opts (Hash) (defaults to: {})


41
42
43
44
45
46
47
48
# 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

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def associations
  @associations
end

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def opts
  @opts
end

#parentObject (readonly)

Returns the value of attribute parent.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def parent
  @parent
end

#propertiesObject (readonly)

Returns the value of attribute properties.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def properties
  @properties
end

#rootObject (readonly)

Returns the value of attribute root.



38
39
40
# File 'lib/frigate/form/association.rb', line 38

def root
  @root
end

Class Method Details

.associationsObject

gets just options for defined associations

Examples:

associations hash

{ some_name: { block: <Proc.new instance> }


33
34
35
# File 'lib/frigate/form/association.rb', line 33

def associations
  @associations.deep_dup || {}
end

.has_one(name, options = {}, &block) ⇒ Object

defines association (kinda property with properties) for association

Parameters:

  • name (Symbol)
  • options (Hash) (defaults to: {})
  • block (Proc)


18
19
20
21
# File 'lib/frigate/form/association.rb', line 18

def has_one(name, options={}, &block)
  @associations ||= {}
  @associations[name.to_sym] = options.merge({ block: block })
end

.propertiesObject

gets just options for defined properties

Examples:

properties hash

{ skype: { validates: { presence: true }, another_option: another_option_hash } }


26
27
28
# File 'lib/frigate/form/association.rb', line 26

def properties
  @properties.deep_dup || {}
end

.property(name, options = {}) ⇒ Object

defines property for association

Parameters:

  • name (Symbol)
  • options (Hash) (defaults to: {})


9
10
11
12
# File 'lib/frigate/form/association.rb', line 9

def property(name, options={})
  @properties ||= {}
  @properties[name.to_sym] = options
end

Instance Method Details

#errorsActiveModel::Errors

gets errors of association

Returns:

  • (ActiveModel::Errors)


52
53
54
# File 'lib/frigate/form/association.rb', line 52

def errors
  @errors ||= ActiveModel::Errors.new(self)
end

#valid?Boolean

checks validness of association properties

Returns:

  • (Boolean)


57
58
59
# File 'lib/frigate/form/association.rb', line 57

def valid?
  errors.messages.empty?
end

#validateObject

validates association properties



62
63
64
# File 'lib/frigate/form/association.rb', line 62

def validate
  properties.each { |_prop| errors.add(_prop.name, _prop.errors[:value]) unless _prop.valid? }
end