Module: SimpleResource::BaseHelper

Defined in:
app/helpers/simple_resource/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#attribute_human_name(attribute_name) ⇒ Object



52
53
54
55
56
# File 'app/helpers/simple_resource/base_helper.rb', line 52

def attribute_human_name(attribute_name)
  attribute_name = attribute_name.to_s
  t("activerecord.attributes.#{controller_name.singularize}.#{attribute_name}",
    default: attribute_name.humanize)
end

#attribute_value(resource, attribute_name, truncation = 50) ⇒ Object



58
59
60
61
62
63
64
65
# File 'app/helpers/simple_resource/base_helper.rb', line 58

def attribute_value(resource, attribute_name, truncation = 50)
  value = resource.send(attribute_name).to_s.truncate(truncation)
  if attribute_name.to_s.match(/_id$/)
    model_name = attribute_name.gsub(/_id$/, "").classify
    value = eval(model_name).find(value).to_s
  end
  value
end

#button_classes_for(action, mini = false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/simple_resource/base_helper.rb', line 93

def button_classes_for(action, mini = false)
  button_classes = Array.new
  button_classes << SimpleResource::Configuration.button_classes
  button_classes << SimpleResource::Configuration.mini_button_classes if mini
  button_classes << (
    SimpleResource::Configuration.respond_to?("button_classes_for_#{action.to_s}") ?
    SimpleResource::Configuration.send("button_classes_for_#{action.to_s}") :
    "")
  button_classes.join(" ").strip
end

#collection_titleObject



15
16
17
18
# File 'app/helpers/simple_resource/base_helper.rb', line 15

def collection_title
  t("activerecord.models.#{controller_name.singularize}.other",
    default: controller_name.humanize)
end

#default_actions_for(resource) ⇒ Object



77
78
79
80
81
82
83
# File 'app/helpers/simple_resource/base_helper.rb', line 77

def default_actions_for(resource)
  html = Array.new
  html << link_to_action(:show, t("simple_resource.links.show"), resource_path(resource))
  html << link_to_action(:edit, t("simple_resource.links.edit"), edit_resource_path(resource))
  html << link_to_action(:delete, t("simple_resource.links.delete"), resource_path(resource))
  html.join("\n").html_safe
end

#edit_resource_titleObject



32
33
34
# File 'app/helpers/simple_resource/base_helper.rb', line 32

def edit_resource_title
  t("simple_resource.titles.edit_resource", resource_name: resource_human_name)
end

#icon_for(action) ⇒ Object



85
86
87
88
89
90
91
# File 'app/helpers/simple_resource/base_helper.rb', line 85

def icon_for(action)
  return "" if !SimpleResource::Configuration.respond_to?("icon_classes_for_#{action.to_s}")
  icon_classes = Array.new
  icon_classes << SimpleResource::Configuration.icon_classes
  icon_classes << SimpleResource::Configuration.send("icon_classes_for_#{action.to_s}")
  (:i, "", class: icon_classes.join(" ").strip).html_safe + " "
end


67
68
69
70
71
72
73
74
75
# File 'app/helpers/simple_resource/base_helper.rb', line 67

def link_to_action(action_name, title, path)
  action_name = action_name.to_sym
  if action_name == :delete
    link_to(icon_for(action_name) + t("simple_resource.#{action_name.to_s}", default: title), path, class: button_classes_for(action_name, true),
      method: :delete, confirm: t("simple_resource.messages.delete_confirmation"))
  else
    link_to(icon_for(action_name) + t("simple_resource.#{action_name.to_s}", default: title), path, class: button_classes_for(action_name, true))
  end
end


28
29
30
# File 'app/helpers/simple_resource/base_helper.rb', line 28

def new_resource_link
  link_to(icon_for(:new) + new_resource_link_title, new_resource_path, class: button_classes_for(:new))
end


24
25
26
# File 'app/helpers/simple_resource/base_helper.rb', line 24

def new_resource_link_title
  t("simple_resource.links.new_resource", resource_name: resource_human_name)
end

#new_resource_titleObject



20
21
22
# File 'app/helpers/simple_resource/base_helper.rb', line 20

def new_resource_title
  t("simple_resource.titles.new_resource", resource_name: resource_human_name)
end

#non_human_attributesObject



40
41
42
# File 'app/helpers/simple_resource/base_helper.rb', line 40

def non_human_attributes
  %w(id updated_at created_at)
end

#render_actions_for(resource) ⇒ Object



119
120
121
# File 'app/helpers/simple_resource/base_helper.rb', line 119

def render_actions_for(resource)
  render "actions", resource: resource
end

#render_collection_table(custom_attributes = nil) ⇒ Object



123
124
125
126
127
# File 'app/helpers/simple_resource/base_helper.rb', line 123

def render_collection_table(custom_attributes = nil)
  render "collection",
    collection: collection,
    attributes: custom_attributes || resource_human_attributes
end

#render_form(form_builder = "formtastic") ⇒ Object



129
130
131
132
133
134
135
# File 'app/helpers/simple_resource/base_helper.rb', line 129

def render_form(form_builder = "formtastic")
  fields = resource_human_attributes
  fields.map! do |arg|
    arg.to_s.sub("_id", "").to_sym
  end
  render "simple_resource/builders/#{form_builder}", fields: fields
end

#resource_attributesObject



36
37
38
# File 'app/helpers/simple_resource/base_helper.rb', line 36

def resource_attributes
  resource_class.attribute_names
end

#resource_form_pathObject



111
112
113
114
115
116
117
# File 'app/helpers/simple_resource/base_helper.rb', line 111

def resource_form_path
  if resource.new_record?
    collection_path
  else
    resource_path
  end
end

#resource_human_attributesObject



44
45
46
47
48
49
50
# File 'app/helpers/simple_resource/base_helper.rb', line 44

def resource_human_attributes
  human_attributes = resource_attributes - non_human_attributes
  if respond_to?("parent?")
    human_attributes = human_attributes - ["#{parent.class.name.underscore}_id"]
  end
  human_attributes
end

#resource_human_name(resource_class_name = nil) ⇒ Object



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

def resource_human_name(resource_class_name = nil)
  if resource_class_name
    eval("#{resource_class_name}.model_name.human")
  else
    resource_class.model_name.human
  end
end

#resource_titleObject



11
12
13
# File 'app/helpers/simple_resource/base_helper.rb', line 11

def resource_title
  "#{resource_human_name} #{resource.id}"
end

#table_classes_for(table_type) ⇒ Object



104
105
106
107
108
109
# File 'app/helpers/simple_resource/base_helper.rb', line 104

def table_classes_for(table_type)
  table_classes = Array.new
  table_classes << SimpleResource::Configuration.table_classes
  table_classes << SimpleResource::Configuration.send("table_classes_for_#{table_type.to_s}")
  table_classes.join(" ").strip
end