Class: SlimFormObject::Base

Inherits:
Object
  • Object
show all
Extended by:
HelperMethods
Includes:
ActiveModel::Model, HelperMethods
Defined in:
lib/slim_form_object/processing.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

assignment_to_each_other, class_name_if_module, define_classes_array_with_name, get_class_of, get_reflection, get_self_object, get_type_and_name_of_association, iterate_parents_with_nested_objects, make_constant_name, method_name_association, snake, to_bind_models, type_and_name_of_association_back_and_forth, type_association

Constructor Details

#initialize(params: {}) ⇒ Base

Returns a new instance of Base.



85
86
87
88
89
90
91
# File 'lib/slim_form_object/processing.rb', line 85

def initialize(params: {})
  require_extensions
  self.base_modules = self.class.base_modules
  self.params       = params
  get_or_add_default_objects
  default_settings
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/slim_form_object/processing.rb', line 77

def method_missing(name, *args, &block)
  if name[/_ids$/]
    model_name, attr_name = get_model_and_method_names(name)
    return send(model_name.to_sym).send(attr_name.to_sym)
  end
  super(name, args, block)
end

Class Attribute Details

.base_modulesObject

Returns the value of attribute base_modules.



10
11
12
# File 'lib/slim_form_object/processing.rb', line 10

def base_modules
  @base_modules
end

Instance Attribute Details

#base_modulesObject

Returns the value of attribute base_modules.



7
8
9
# File 'lib/slim_form_object/processing.rb', line 7

def base_modules
  @base_modules
end

#data_objects_arrObject

Returns the value of attribute data_objects_arr.



7
8
9
# File 'lib/slim_form_object/processing.rb', line 7

def data_objects_arr
  @data_objects_arr
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/slim_form_object/processing.rb', line 7

def params
  @params
end

Class Method Details

.input_data_structure(**structure) ⇒ Object

data_structure



21
22
23
24
25
26
27
# File 'lib/slim_form_object/processing.rb', line 21

def input_data_structure(**structure)
  instance_eval do
    define_method(:data_structure) { structure }
  end
  
  define_array_of_models(:array_of_all_models, get_main_models_from_structure(structure))
end

.set_model_name(name) ⇒ Object



12
13
14
# File 'lib/slim_form_object/processing.rb', line 12

def set_model_name(name)
  define_method(:model_name) { ActiveModel::Name.new(self, nil, name) }
end

Instance Method Details

#apply_parametersObject Also known as: submit

END INIT



94
95
96
97
# File 'lib/slim_form_object/processing.rb', line 94

def apply_parameters
  apply
  self
end

#permit_params(params) ⇒ Object



112
113
114
115
# File 'lib/slim_form_object/processing.rb', line 112

def permit_params(params)
  return {} if params.empty?
  params.require(snake(model_name.name).gsub(/_+/, '_')).permit(data_structure)
end

#saveObject



100
101
102
# File 'lib/slim_form_object/processing.rb', line 100

def save
  Saver.new(self).save
end

#save!Object



104
105
106
# File 'lib/slim_form_object/processing.rb', line 104

def save!
  Saver.new(self).save!
end

#validation_modelsObject



108
109
110
# File 'lib/slim_form_object/processing.rb', line 108

def validation_models
  Validator.new(self).validate_form_object
end