Module: SmarterListing::Helper

Defined in:
lib/smarter_listing/helper.rb

Instance Method Summary collapse

Instance Method Details

#_resource_paramsObject



26
27
28
29
30
# File 'lib/smarter_listing/helper.rb', line 26

def _resource_params
  method = "#{resource_sym}_params".to_sym
  return send method if respond_to? method
  resource_params
end

#action_copy(object, path) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/smarter_listing/helper.rb', line 81

def action_copy(object, path)
  {
      name: :copy,
      custom_url: path,
      custom_icon: SmartListing.config.classes(:icon_copy),
      custom_title: 'Copy'
  }
end

#action_destroy(object, path) ⇒ Object



94
95
96
# File 'lib/smarter_listing/helper.rb', line 94

def action_destroy(object, path)
  {name: :destroy, url: path}
end

#action_edit(object, path) ⇒ Object



90
91
92
# File 'lib/smarter_listing/helper.rb', line 90

def action_edit(object, path)
  {name: :edit, url: path}
end

#collection_ivarObject



8
9
10
# File 'lib/smarter_listing/helper.rb', line 8

def collection_ivar
  "@#{collection_sym}"
end

#collection_symObject



4
5
6
# File 'lib/smarter_listing/helper.rb', line 4

def collection_sym
  @collection_sym ||= model.to_s.underscore.sub('/','_').pluralize.to_sym
end

#current_engineObject



36
37
38
39
# File 'lib/smarter_listing/helper.rb', line 36

def current_engine
  object = instance_variables.include?(:@_controller) ? @_controller : self
  object.class.module_parent == Object ? '' : object.class.module_parent.to_s.underscore
end

#modelObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/smarter_listing/helper.rb', line 41

def model
  controller = instance_variables.include?(:@_controller) ? @_controller : self
  @model ||= begin
               # First priority is the namespaced model, e.g. User::Group
    resource_class ||= begin
      namespaced_class = controller.class.name.sub(/Controller/, '').singularize
      namespaced_class.constantize
    rescue NameError
      nil
    end

    # Second priority is the top namespace model, e.g. EngineName::Article for EngineName::Admin::ArticlesController
    resource_class ||= begin
      namespaced_classes = controller.class.name.sub(/Controller/, '').split('::')
      namespaced_class = [namespaced_classes.first, namespaced_classes.last].join('::').singularize
      namespaced_class.constantize
    rescue NameError
      nil
    end

    # Third priority the camelcased c, i.e. UserGroup
    resource_class ||= begin
      camelcased_class = controller.class.name.sub(/Controller/, '').gsub('::', '').singularize
      camelcased_class.constantize
    rescue NameError
      nil
    end

    # Otherwise use the Group class, or fail
    resource_class ||= begin
      class_name = controller.controller_name.classify
      class_name.constantize
    rescue NameError => e
      raise unless e.message.include?(class_name)
      nil
    end
    resource_class
  end
end

#resource_ivarObject



22
23
24
# File 'lib/smarter_listing/helper.rb', line 22

def resource_ivar
  "@#{resource_sym}"
end

#resource_symObject



12
13
14
# File 'lib/smarter_listing/helper.rb', line 12

def resource_sym
  @resource_sym ||= model.to_s.underscore.sub('/','_').to_sym
end

#resource_view_pathObject



16
17
18
19
20
# File 'lib/smarter_listing/helper.rb', line 16

def resource_view_path
  path = model.name.tableize
  path = "#{current_engine}/#{path}" unless path.start_with?("#{current_engine}/")
  path
end

#table_nameObject



32
33
34
# File 'lib/smarter_listing/helper.rb', line 32

def table_name
  model.name.demodulize.tableize.to_sym
end