class Frigate::Form::Base
Is used for model validation outside of model
@example 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.build_user_profile user_form = UserForm.new(user) user_form.valid? # returns false user_form.validate({email: 'z1@zzz.zz', user_profile: {first_name:'Alpha', last_name:'Omega'}}) # returns true user_form.valid? # returns true
@author Vladislav Pauk <vladislavpauk@gmail.com>
Attributes
Public Class Methods
gets just options for defined associations @example associations hash
{ some_name: { block: <Proc.new instance> }
# File lib/frigate/form/base.rb, line 66 def associations @associations.deep_dup || {} end
defines association (kinda property with properties)(root as well) of form/base @param [Symbol] name @param [Hash] options @param [Proc] block
# File lib/frigate/form/base.rb, line 82 def has_one(name, options={}, &block) @associations ||= {} @associations[name.to_sym] = options.merge({ block: block }) end
Method of initialization of Frigate::Form
class @param [Object] model
# 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
gets just options for defined properties @example properties hash
{ skype: { validates: { presence: true }, another_option: another_option_hash } }
# File lib/frigate/form/base.rb, line 59 def properties @properties.deep_dup || {} end
defines property (root) of form/base @param [Symbol] name @param [Hash] options
# File lib/frigate/form/base.rb, line 73 def property(name, options={}) @properties ||= {} @properties[name.to_sym] = options end
Public Instance Methods
Initializes errors instance variable of ActiveModel::Errors type @return [ActiveModel::Errors] return errors
# File lib/frigate/form/base.rb, line 103 def errors @errors ||= ActiveModel::Errors.new(self) end
returns true if are there any errors messages in form errors @return [Boolean] return true if form model is valid
# File lib/frigate/form/base.rb, line 116 def valid? errors.messages.empty? end
Validates form with params or model properties
# File lib/frigate/form/base.rb, line 108 def validate(*args) sync_properties_with_model_or_params(*args) sync_errors valid? end
Private Instance Methods
initializes declared associations
# File lib/frigate/form/base.rb, line 123 def process_associations self.class.associations.each do |_assoc_name, _assoc_options| _assoc = Association.new(_assoc_name, self, self, _assoc_options) @associations << _assoc define_singleton_method(_assoc.name) { _assoc } end end
initializes declared properties
# File lib/frigate/form/base.rb, line 132 def process_properties self.class.properties.each do |_prop_name, _prop_options| _prop = Property.new(_prop_name, self, self, _prop_options) @properties << _prop define_singleton_method(_prop.name) { _prop } end end