lib/freeform/form/property.rb in freeform-1.0.11 vs lib/freeform/form/property.rb in freeform-2.0.0

- old
+ new

@@ -1,51 +1,16 @@ +require 'freeform/form/date_params_filter' + module FreeForm module Property def self.included(base) base.extend(ClassMethods) end module ClassMethods include Forwardable - # Models - #------------------------------------------------------------------------ - def models - @models ||= [] - end - - def child_models - @child_models ||= [] - end - - def declared_model(name, opts={}) - @models ||= [] - @models << name - attr_accessor name - end - alias_method :form_model, :declared_model - - def declared_models(*names) - names.each do |name| - declared_model(name) - end - end - alias_method :form_models, :declared_models - - def child_model(name, opts={}, &block) - @models ||= [] - @models << name - @child_models ||= [] - @child_models << name - attr_accessor name - define_method("initialize_#{name}") do - instance_variable_set("@#{name}", instance_eval(&block)) - end - end - - # Properties - #------------------------------------------------------------------------ def property_mappings # Take the form of {:property => {:model => model, :field => field, :ignore_blank => false}} @property_mappings ||= Hash.new end @@ -87,59 +52,50 @@ end end end end - attr_accessor :parent_form - - class DateParamsFilter - def call(params) - date_attributes = {} - - params.each do |attribute, value| - if value.is_a?(Hash) - call(value) # TODO: #validate should only handle local form params. - elsif matches = attribute.match(/^(\w+)\(.i\)$/) - date_attribute = matches[1] - date_attributes[date_attribute] = params_to_date( - params.delete("#{date_attribute}(1i)"), - params.delete("#{date_attribute}(2i)"), - params.delete("#{date_attribute}(3i)") - ) - end + def assign_params(params) + self.tap do |s| + FreeForm::DateParamsFilter.new.call(params) + before_assign_params(params) + params.each_pair do |attribute, value| + assign_attribute(attribute, value) end - params.merge!(date_attributes) + after_assign_params(params) end - - private - def params_to_date(year, month, day) - day ||= 1 # FIXME: is that really what we want? test. - begin # TODO: test fails. - return Date.new(year.to_i, month.to_i, day.to_i) - rescue ArgumentError => e - return nil - end - end end - - def assign_params(params) - DateParamsFilter.new.call(params) - params.each_pair do |attribute, value| - assign_attribute(attribute, value) - end - self - end alias_method :assign_attributes, :assign_params alias_method :populate, :assign_params alias_method :fill, :assign_params + def before_assign_params(params) + end + alias_method :before_assign_attributes, :before_assign_params + alias_method :before_populate, :before_assign_params + alias_method :before_fill, :before_assign_params + + def after_assign_params(params) + end + alias_method :after_assign_attributes, :after_assign_params + alias_method :after_populate, :after_assign_params + alias_method :after_fill, :after_assign_params + + def model_property_mappings + self.class.property_mappings + end + private def assign_attribute(attribute, value) self.send :"#{attribute}=", value unless ignore?(attribute, value) end def ignore?(attribute, value) - mapping = self.class.property_mappings[attribute.to_sym] - return (mapping[:ignore_blank] && value.blank?) unless mapping.nil? + mapping = model_property_mappings[attribute.to_sym] + if mapping.present? + mapping[:ignore_blank] && value.blank? + else + false + end end end end