Module: Carload::ApplicationHelper

Included in:
DashboardController
Defined in:
app/helpers/carload/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#associated_model_name(model_name, attribute_name) ⇒ Object



23
24
25
26
27
28
29
# File 'app/helpers/carload/application_helper.rb', line 23

def associated_model_name model_name, attribute_name
  x = attribute_name.gsub(/_ids?$/, '').to_sym
  Dashboard.model(model_name).associations.each do |name, association|
    return association[:class_name] || x, name if name.to_s.singularize.to_sym == x
  end
  raise 'Should not go here!'
end

#id_or_ids(reflection) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/helpers/carload/application_helper.rb', line 31

def id_or_ids reflection
  case reflection
  when ActiveRecord::Reflection::HasManyReflection
    "#{reflection.name.to_s.singularize}_ids"
  else
    "#{reflection.name}_id"
  end
end

#image?(model_name, attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'app/helpers/carload/application_helper.rb', line 19

def image? model_name, attribute_name
  attribute_name.to_s =~ /image|logo|img/ or (Dashboard.model(model_name).attributes.images.include? attribute_name.to_sym rescue nil)
end

#needs_upload?(model_name, attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
# File 'app/helpers/carload/application_helper.rb', line 3

def needs_upload? model_name, attribute_name
  case Carload.upload_solution
  when :carrierwave
    model_class = model_name.to_s.classify.constantize
    not model_class.instance_methods.map(&:to_s).select { |x| x =~ /#{attribute_name}_url/ }.empty?
  end
end

#polymorphic?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'app/helpers/carload/application_helper.rb', line 11

def polymorphic? attribute_name
  Dashboard.model(@model_name).associations.each_value do |association|
    reflection = association[:reflection]
    return reflection.name if attribute_name =~ /#{reflection.name}/ and reflection.options[:polymorphic]
  end
  false
end