Module: HelperMethods
- Included in:
- ActionView::Helpers::DateTimeSelector, SlimFormObject::Assign, SlimFormObject::Base, SlimFormObject::Base, SlimFormObject::DataObject, SlimFormObject::Saver, SlimFormObject::Validator
- Defined in:
- lib/slim_form_object/helpers.rb,
lib/slim_form_object/form_helpers/extension_actionview.rb
Instance Method Summary collapse
- #assignment_to_each_other(object_1, object_2) ⇒ Object
- #class_name_if_module(string) ⇒ Object
- #define_classes_array_with_name(name, args) ⇒ Object
- #get_class_of(snake_model_name, base_module = nil) ⇒ Object
- #get_reflection(class1, class2) ⇒ Object
- #get_self_object(model) ⇒ Object
- #get_type_and_name_of_association(class1, class2) ⇒ Object
- #iterate_parents_with_nested_objects(data_objects, &block) ⇒ Object
- #make_constant_name(snake_model_name, base_module = nil) ⇒ Object
- #method_name_association(reflection) ⇒ Object
- #snake(string) ⇒ Object
- #to_bind_models(object_1, object_2) ⇒ Object
- #type_and_name_of_association_back_and_forth(class1, class2) ⇒ Object
- #type_association(reflection) ⇒ Object
Instance Method Details
#assignment_to_each_other(object_1, object_2) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/slim_form_object/helpers.rb', line 29 def assignment_to_each_other(object_1, object_2) type, method_name = get_type_and_name_of_association(object_1.class, object_2.class) if type == :belongs_to or type == :has_one object_1.send( "#{method_name.to_s}=", object_2 ) elsif type == :has_many or type == :has_and_belongs_to_many object_1.method(method_name).call << object_2 end end |
#class_name_if_module(string) ⇒ Object
76 77 78 79 |
# File 'lib/slim_form_object/helpers.rb', line 76 def class_name_if_module(string) return $1 if string =~ /^.+::(.+)$/ string end |
#define_classes_array_with_name(name, args) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/slim_form_object/helpers.rb', line 81 def define_classes_array_with_name(name, args) args.each { |model| raise "#{model.to_s} - type is not a Class" if model.class != Class } instance_eval do define_method(name) { args } end end |
#get_class_of(snake_model_name, base_module = nil) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/slim_form_object/helpers.rb', line 11 def get_class_of(snake_model_name, base_module=nil) Module.const_get( make_constant_name(snake_model_name, base_module) ) rescue NameError => ex unless ex.class == NameError raise ex end end |
#get_reflection(class1, class2) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/slim_form_object/helpers.rb', line 44 def get_reflection(class1, class2) class1.reflections.select do |k,v| if v&.&.send(:[], :polymorphic) temp_reflections = class2.reflections.select do |k,v| v&.&.send(:[], :polymorphic) ? false : (v.klass == class1) end.values !temp_reflections.empty? else v.klass == class2 end end.values.first end |
#get_self_object(model) ⇒ Object
2 3 4 |
# File 'lib/slim_form_object/helpers.rb', line 2 def get_self_object(model) method( snake(model.to_s).to_sym ).call end |
#get_type_and_name_of_association(class1, class2) ⇒ Object
39 40 41 42 |
# File 'lib/slim_form_object/helpers.rb', line 39 def get_type_and_name_of_association(class1, class2) reflection = get_reflection(class1, class2) [type_association(reflection), method_name_association(reflection)] end |
#iterate_parents_with_nested_objects(data_objects, &block) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/slim_form_object/helpers.rb', line 88 def iterate_parents_with_nested_objects(data_objects, &block) data_objects.each do |data_object| iterate_parents_with_nested_objects(data_object.nested, &block) data_object.nested.each do |nested_data_object| block.call(data_object, nested_data_object) if block end end end |
#make_constant_name(snake_model_name, base_module = nil) ⇒ Object
6 7 8 9 |
# File 'lib/slim_form_object/helpers.rb', line 6 def make_constant_name(snake_model_name, base_module=nil) pref = base_module ? (base_module.to_s + '::') : '' pref + snake_model_name.to_s.split('_').map(&:capitalize).join end |
#method_name_association(reflection) ⇒ Object
62 63 64 |
# File 'lib/slim_form_object/helpers.rb', line 62 def method_name_association(reflection) reflection&.name end |
#snake(string) ⇒ Object
70 71 72 73 74 |
# File 'lib/slim_form_object/helpers.rb', line 70 def snake(string) string = string.to_s string.gsub!(/((\w)([A-Z]))/,'\2_\3') class_name_if_module(string.downcase) end |
#to_bind_models(object_1, object_2) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/slim_form_object/helpers.rb', line 19 def to_bind_models(object_1, object_2) if object_1.new_record? assignment_to_each_other(object_1, object_2) object_1 else assignment_to_each_other(object_2, object_1) object_2 end end |
#type_and_name_of_association_back_and_forth(class1, class2) ⇒ Object
66 67 68 |
# File 'lib/slim_form_object/helpers.rb', line 66 def type_and_name_of_association_back_and_forth(class1, class2) get_type_and_name_of_association(class1, class2) + get_type_and_name_of_association(class2, class1) end |
#type_association(reflection) ⇒ Object
58 59 60 |
# File 'lib/slim_form_object/helpers.rb', line 58 def type_association(reflection) reflection&.macro end |