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
- #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
68 69 70 71 |
# File 'lib/slim_form_object/helpers.rb', line 68 def class_name_if_module(string) return $1 if string =~ /^.+::(.+)$/ string end |
#define_classes_array_with_name(name, args) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/slim_form_object/helpers.rb', line 73 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
6 7 8 9 10 11 |
# File 'lib/slim_form_object/helpers.rb', line 6 def get_class_of(snake_model_name, base_module=nil) pref = base_module ? (base_module.to_s + '::') : '' Module.const_get( pref + snake_model_name.to_s.split('_').map(&:capitalize).join ) rescue nil end |
#get_reflection(class1, class2) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/slim_form_object/helpers.rb', line 38 def get_reflection(class1, class2) class1.reflections.select do |k,v| if v&.&.send(:[], :polymorphic) if class2.reflections.select{ |k,v| v.klass == class1 }.values.first true end 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
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
54 55 56 |
# File 'lib/slim_form_object/helpers.rb', line 54 def method_name_association(reflection) reflection&.name end |
#snake(string) ⇒ Object
62 63 64 65 66 |
# File 'lib/slim_form_object/helpers.rb', line 62 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_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
58 59 60 |
# File 'lib/slim_form_object/helpers.rb', line 58 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
50 51 52 |
# File 'lib/slim_form_object/helpers.rb', line 50 def type_association(reflection) reflection&.macro end |