module PathUtilities::Form
Attributes
models_mapping[R]
Public Class Methods
new(models_mapping = {})
click to toggle source
# File lib/path_utilities/form.rb, line 28 def initialize(models_mapping = {}) @models_mapping = models_mapping validate_mapping! init_sync end
setup_model_name(name)
click to toggle source
# File lib/path_utilities/form.rb, line 46 def self.setup_model_name(name) self.model_name = ActiveModel::Name.new(self, nil, name.to_s) end
Public Instance Methods
add_model(name)
click to toggle source
# File lib/path_utilities/form.rb, line 110 def add_model(name) return if self.models.include?(name.to_sym) existing_models = models existing_models << name.to_sym self.models = existing_models end
already_define_attribute_warn(attr, new_model) { || ... }
click to toggle source
# File lib/path_utilities/form.rb, line 123 def already_define_attribute_warn(attr, new_model) if fields[attr].nil? yield else Rails.logger.warn "#{attr} param already defined " \ "for #{fields[attr]} model" return unless fields[attr] != new_model Rails.logger.warn "#{attr} now is mapped to #{new_model}" end end
database_field_name(attribute)
click to toggle source
mongoid-encrypted-fields gem compatibility method
# File lib/path_utilities/form.rb, line 157 def database_field_name(attribute) attribute.to_sym end
init_sync()
click to toggle source
# File lib/path_utilities/form.rb, line 80 def init_sync self.class.fields.keys.each do |field| model_value = instance_model_for(field).send("#{field}") send("#{field}=", model_value) end end
instance_model_for(field)
click to toggle source
# File lib/path_utilities/form.rb, line 75 def instance_model_for(field) model = self.class.fields[field].options[:klass].name.underscore.to_sym models_mapping[model] end
main_record()
click to toggle source
# File lib/path_utilities/form.rb, line 87 def main_record self.class.model_name || fail('setup_model_name not set in form class') models_mapping[self.class.model_name.name.to_sym] end
model_for(attr)
click to toggle source
# File lib/path_utilities/form.rb, line 152 def model_for(attr) fields[attr] && fields[attr].options[:klass] end
properties(attributes, model)
click to toggle source
# File lib/path_utilities/form.rb, line 98 def properties(attributes, model) add_model(model) attributes.each do |att| already_define_attribute_warn(att, model) do attribute att, String end set_attribute(att, model.to_s.camelize .safe_constantize.fields[att.to_s]) end end
save()
click to toggle source
# File lib/path_utilities/form.rb, line 58 def save if valid? sync persist! true else false end end
set_attribute(name, value)
click to toggle source
# File lib/path_utilities/form.rb, line 117 def set_attribute(name, value) existing_fields = self.fields existing_fields[name] = value self.fields = existing_fields end
sync()
click to toggle source
# File lib/path_utilities/form.rb, line 68 def sync self.class.fields.keys.each do |field| form_field_value = send(field) instance_model_for(field).send("#{field}=", form_field_value) end end
validate(params)
click to toggle source
# File lib/path_utilities/form.rb, line 34 def validate(params) params = HashWithIndifferentAccess.new(params) self.class.fields.keys.each do |field| any_changes = params[field] && params[field] != instance_model_for(field).send(field) next unless any_changes send("#{field}=", params[field]) end valid? end
validate_mapping!()
click to toggle source
# File lib/path_utilities/form.rb, line 51 def validate_mapping! self.class.models.each do |model| next if models_mapping.keys.include?(model) fail "#{model.to_s.camelize} not mapped on initialization" end end
validates_uniqueness_of(attribute, options = {})
click to toggle source
# File lib/path_utilities/form.rb, line 135 def validates_uniqueness_of(attribute, options = {}) options = { case_sensitive: false, attributes: [attribute], model: model_for(attribute) } .reverse_merge(options) validates_with(validation_class_for(attribute), options) end
validation_class_for(attr)
click to toggle source
# File lib/path_utilities/form.rb, line 142 def validation_class_for(attr) model_klass = model_for(attr) case when model_klass.ancestors.include?(::Mongoid::Document) 'PathUtilities::Form::UniquenessValidator::Mongoid' else fail "#{model_klass.name} is not currently supported" end.safe_constantize end
Private Instance Methods
persist!()
click to toggle source
# File lib/path_utilities/form.rb, line 92 def persist! models_mapping.values.all?(&:save) end