Module: HelperMethods
- Included in:
- ActionView::Helpers::DateTimeSelector, ActionView::Helpers::FormBuilder, ActionView::Helpers::FormHelper, ActionView::Helpers::Tags::Base, SlimFormObject::Assign, SlimFormObject::Base, SlimFormObject::Base, 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
- #get_class_of(snake_model_name, base_modulenil) ⇒ Object
- #get_reflection(class1, class2) ⇒ Object
- #get_self_object(model) ⇒ Object
- #get_type_and_name_of_association(class1, class2) ⇒ 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
23 24 25 26 27 28 29 30 31 |
# File 'lib/slim_form_object/helpers.rb', line 23 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
60 61 62 63 |
# File 'lib/slim_form_object/helpers.rb', line 60 def class_name_if_module(string) return $1 if string =~ /^.+::(.+)$/ string end |
#get_class_of(snake_model_name, base_modulenil) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/slim_form_object/helpers.rb', line 6 def get_class_of(snake_model_name, base_modulenil) pref = base_module ? (base_module.to_s + '::') : '' Object.const_get( pref + snake_model_name.to_s.split('_').map(&:capitalize).join ) rescue nil end |
#get_reflection(class1, class2) ⇒ Object
38 39 40 |
# File 'lib/slim_form_object/helpers.rb', line 38 def get_reflection(class1, class2) class1.reflections.select{ |k,v| v.klass == class2 }.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
33 34 35 36 |
# File 'lib/slim_form_object/helpers.rb', line 33 def get_type_and_name_of_association(class1, class2) reflection = get_reflection(class1, class2) [type_association(reflection), method_name_association(reflection)] end |
#method_name_association(reflection) ⇒ Object
46 47 48 |
# File 'lib/slim_form_object/helpers.rb', line 46 def method_name_association(reflection) reflection&.name end |
#snake(string) ⇒ Object
54 55 56 57 58 |
# File 'lib/slim_form_object/helpers.rb', line 54 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
13 14 15 16 17 18 19 20 21 |
# File 'lib/slim_form_object/helpers.rb', line 13 def to_bind_models(object_1, object_2) if object_2.new_record? assignment_to_each_other(object_2, object_1) else assignment_to_each_other(object_1, object_2) end object_1 end |
#type_and_name_of_association_back_and_forth(class1, class2) ⇒ Object
50 51 52 |
# File 'lib/slim_form_object/helpers.rb', line 50 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
42 43 44 |
# File 'lib/slim_form_object/helpers.rb', line 42 def type_association(reflection) reflection&.macro end |