Module: HelperMethods

Instance Method Summary collapse

Instance Method Details

#apply_expression_text(string, exp) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/slim_form_object/helpers.rb', line 15

def apply_expression_text(string, exp)
  string[exp]
  model_name = $1
  attr_name  = $2

  [model_name, attr_name]
end

#get_association(class1, class2) ⇒ Object



41
42
43
# File 'lib/slim_form_object/helpers.rb', line 41

def get_association(class1, class2)
  class1.reflections.slice(snake(class2.to_s), class2.table_name).values.first&.macro
end

#get_class_of_snake_model_name(snake_model_name) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/slim_form_object/helpers.rb', line 6

def get_class_of_snake_model_name(snake_model_name)
  pref = if self.base_module
    self.base_module.to_s + '::'
  else
    ''
  end
  Object.const_get( pref + snake_model_name.to_s.split('_').map(&:capitalize).join )
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

#snake(string) ⇒ Object



23
24
25
26
27
# File 'lib/slim_form_object/helpers.rb', line 23

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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slim_form_object/helpers.rb', line 29

def to_bind_models(object_1, object_2)
  association = get_association(object_1.class, object_2.class)

  if    association == :belongs_to or association == :has_one
    object_1.send( "#{snake(object_2.class.to_s)}=", object_2 )
  elsif association == :has_many   or association == :has_and_belongs_to_many
    object_1.method("#{object_2.class.table_name}").call << object_2
  end

  object_1
end