Class: Frigate::Form::Base

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

Overview

Is used for model validation outside of model

Examples:

A form for User class

class UserForm < Frigate::Form::Base
  property :email, validates: { presence: true }
  has_one :user_profile do
    property :first_name, validates: { presence: true }
    property :last_name
    property :skype
  end
end

class UserFormCustom < Frigate::Form::Base
  property :email, validates: { presence: true },
    validate: [
      Proc.new { error(:invalid) unless value =~ /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i },
    ]
  has_one :user_profile do
    property :first_name, validates: { presence: true }
    property :last_name
    property :skype
  end
end

class UserFormTree < Frigate::Form::Base
  property :email
  has_one :user_profile do
    property :first_name
    property :last_name
    property :skype
    has_one :user_profile_passport do
      property :number
      property :country
      property :city
    end
  end
end

user = User.new
user.
user_form = UserForm.new(user)
user_form.valid? # returns false
user_form.validate({email: '[email protected]', user_profile: {first_name:'Alpha', last_name:'Omega'}}) # returns true
user_form.valid? # returns true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, opts = {}) ⇒ Base

Method of initialization of Frigate::Form class

Parameters:



93
94
95
96
97
98
99
# File 'lib/frigate/form/base.rb', line 93

def initialize(model, opts={})
  @syncronizator = opts[:contract] ? Synchronizer::Contract.new(self) : Synchronizer::Form.new(self)
  @model, @properties, @associations = model, [], []

  process_properties
  process_associations
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



88
89
90
# File 'lib/frigate/form/base.rb', line 88

def associations
  @associations
end

#modelObject (readonly)

Returns the value of attribute model.



88
89
90
# File 'lib/frigate/form/base.rb', line 88

def model
  @model
end

#propertiesObject (readonly)

Returns the value of attribute properties.



88
89
90
# File 'lib/frigate/form/base.rb', line 88

def properties
  @properties
end

Class Method Details

.associationsObject

gets just options for defined associations

Examples:

associations hash

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


66
67
68
# File 'lib/frigate/form/base.rb', line 66

def associations
  @associations.deep_dup || {}
end

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

defines association (kinda property with properties)(root as well) of form/base

Parameters:

  • (defaults to: {})


82
83
84
85
# File 'lib/frigate/form/base.rb', line 82

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 } }


59
60
61
# File 'lib/frigate/form/base.rb', line 59

def properties
  @properties.deep_dup || {}
end

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

defines property (root) of form/base

Parameters:

  • (defaults to: {})


73
74
75
76
# File 'lib/frigate/form/base.rb', line 73

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

Instance Method Details

#errorsActiveModel::Errors

Initializes errors instance variable of ActiveModel::Errors type

Returns:

  • return errors



103
104
105
# File 'lib/frigate/form/base.rb', line 103

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

#valid?Boolean

returns true if are there any errors messages in form errors

Returns:

  • return true if form model is valid



116
117
118
# File 'lib/frigate/form/base.rb', line 116

def valid?
  errors.messages.empty?
end

#validate(*args) ⇒ Object

Validates form with params or model properties



108
109
110
111
112
# File 'lib/frigate/form/base.rb', line 108

def validate(*args)
  sync_properties_with_model_or_params(*args)
  sync_errors
  valid?
end