Module: Tramway::Core::Associations::ObjectHelper

Included in:
Tramway::Core::ApplicationDecorator
Defined in:
app/decorators/tramway/core/associations/object_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_association_form_class_name(object, association_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 38

def add_association_form_class_name(object, association_name)
  form_class = "Admin::#{object.class.to_s.pluralize}::Add#{association_name.to_s.camelize.singularize}Form"

  begin
    form_class.constantize
  rescue StandardError
    Tramway::Error.raise_error(
      :tramway, :core, :associations, :object_helper, :habtm_add_class_not_defined,
      class: form_class, association_name: association_name
    )
  end
end

#association_has_one_without_object(object, association_name, association_type) ⇒ Object



51
52
53
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 51

def association_has_one_without_object(object, association_name, association_type)
  association_type == :has_one && object.send(association_name).nil?
end

#association_object(object, association_name) ⇒ Object



55
56
57
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 55

def association_object(object, association_name)
  object.send association_name
end

#association_type(association) ⇒ Object



28
29
30
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 28

def association_type(association)
  association.class.to_s.split('::').last.sub(/Reflection$/, '').underscore.to_sym
end

#associations_collection(object, association_name, decorator_class_name) ⇒ Object



32
33
34
35
36
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 32

def associations_collection(object, association_name, decorator_class_name)
  object.send(association_name).active.map do |association_object|
    decorator_class_name.decorate association_object
  end
end

#check_association(object, association_name, association) ⇒ Object



19
20
21
22
23
24
25
26
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 19

def check_association(object, association_name, association)
  return unless association.nil?

  Tramway::Error.raise_error(
    :tramway, :core, :associations, :class_helper, :model_does_not_have_association,
    object_class: object.class, association_name: association_name
  )
end

#class_name(association) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/decorators/tramway/core/associations/object_helper.rb', line 4

def class_name(association)
  if association.polymorphic?
    object.send(association.name).class
  else
    unless association.options[:class_name]
      Tramway::Error.raise_error(
        :tramway, :core, :associations, :object_helper, :please_specify_association_name,
        association_name: association.name, object_class: object.class,
        association_class_name: association.name.to_s.singularize.camelize
      )
    end
    association.options[:class_name]
  end
end