Module: GeoLabels::ApplicationHelper

Includes:
RatingHelper
Defined in:
app/helpers/geo_labels/application_helper.rb

Instance Method Summary collapse

Methods included from RatingHelper

#show_rating_for

Instance Method Details

#active_class(*route_specs) ⇒ Object

Return active or nil based on the given route spec

%li{ class: active_class('users') # true if controller is users, false otherwise
%li{ class: active_class('pages#about') # true if controller is pages and action is about

NOTE: Taken from the dunlop-core gem. That is the best maintained version



34
35
36
37
38
39
40
# File 'app/helpers/geo_labels/application_helper.rb', line 34

def active_class(*route_specs)
  options = route_specs.extract_options!
  return nil if Array.wrap(options[:except]).any?{ |exception| current_route_spec?(exception) }
  return 'active' if route_specs.any?{ |rs| current_route_spec?(rs, options) }

  nil
end

#address_tag(address, title: nil) ⇒ Object



211
212
213
214
# File 'app/helpers/geo_labels/application_helper.rb', line 211

def address_tag(address, title: nil)
  tag.span(' '.html_safe * 10) +
    tag.div(address, class: 'ui basic tag label', title: title)
end

#at(attribute_name, scope_model = nil) ⇒ Object



137
138
139
140
# File 'app/helpers/geo_labels/application_helper.rb', line 137

def at(attribute_name, scope_model = nil)
  scope_model ||= @scope_model
  scope_model.human_attribute_name(attribute_name)
end

#current_route_spec?(route_spec, _options = {}) ⇒ Boolean

Check if the current route matches the route given as argument. The syntax is meant to be a bit similar to specifying routes in ‘config/routes.rb`.

current_route_spec?('products') #=> true if controller name is products, false otherwise
current_route_spec?('products#show') #=> true if controller_name is products AND action_name is show
current_route_spec?('#show') #=> true if action_name is show, false otherwise

NOTE: this helper is tested through the active_class helper NOTE: Taken from the dunlop-core gem. That is the best maintained version

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/geo_labels/application_helper.rb', line 50

def current_route_spec?(route_spec, _options = {})
  return route_spec.match([controller_path, action_name].join('#')) if route_spec.is_a?(Regexp)

  controller, action = route_spec.split('#')
  return action == params[:id] if controller_path == 'high_voltage/pages'

  actual_controller_parts = controller_path.split('/')
  if controller # and controller_path == controller
    tested_controller_parts = controller.split('/')
    return if tested_controller_parts.size > actual_controller_parts.size

    if actual_controller_parts[0...tested_controller_parts.size] == tested_controller_parts
      # controller spec matches
      return true unless action

      action_name == action
    end
  else
    action_name == action
  end
end

#flash_class(level) ⇒ Object



217
218
219
220
221
222
223
224
# File 'app/helpers/geo_labels/application_helper.rb', line 217

def flash_class(level)
  case level.to_sym
  when :success       then 'ui positive message'
  when :error, :alert then 'ui negative message'
  when :notice        then 'ui info message'
  else "ui #{level} message"
  end
end


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/geo_labels/application_helper.rb', line 7

def link_to_labels_tree
  # output = "<ul class='ui list'>"
  output = tag.ul(class: 'ui list labels-tree')[0..-6]
  #add_punctuation = '*'
  tree_formatter = proc do |items, lead_space|
    items.each do |item|
      label_name = link_to(item[:name], label_path(item[:id]))
      edit_link  = table_edit_link(item, geo_labels.edit_label_path(item[:id]))
      add_child_link = link_to(tag.i(class: 'plus icon'), geo_labels.new_label_path(parent_id: item[:id]))
      output += "#{lead_space}#{tag.li(label_name + add_child_link + edit_link)}\n".html_safe
      next unless item[:children].present?

      output += "#{lead_space}<ul>".html_safe
      tree_formatter.call(item[:children], "#{lead_space}  ")
      output += "#{lead_space}</ul>".html_safe
    end
  end
  tree_formatter.call(GeoLabels::Exporter.labels_tree_h, '')
  # output.gsub!(/^(\s*)  /, "\1#{add_punctuation} ") if add_punctuation.present?
  output += '</ul>'.html_safe
  output.html_safe
end

#page_title(*args, &blk) ⇒ Object

NOTE: Taken from the dunlop-core gem. That is the best maintained version



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/geo_labels/application_helper.rb', line 73

def page_title(*args, &blk)
  extra_content = capture(&blk) if blk.present?
  if resource_title?(args)
    @scope_model = args[1].is_a?(ActiveRecord::Base) ? args[1].class : args[1]
    content = page_title_for_resource(args)
  else
    content = page_title_for_string(args)
  end
  content += extra_content if extra_content.present?
  title_tag = (:h2, content, class: 'page-title ui header')
  content_for :page_title, title_tag
  title_tag
end

#page_title_for_resource(args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/geo_labels/application_helper.rb', line 107

def page_title_for_resource(args)
  options = args.extract_options!
  model = args[1].respond_to?(:model_name) ? args[1] : args[1].class
  title = if args.first == :index
            t('action.index.label', models: model.model_name.human_plural).html_safe
          else
            t("action.#{args.first}.label", model: model.model_name.human).html_safe
          end
  if back_options = options[:back]
    url =
      case back_options
      when Array then polymorphic_path(back_options)
      when true then :back
      else back_options
      end
    if url
      back_link = link_to (:i, nil, class: 'left arrow icon'), url, class: 'title-back-link'
      title = back_link.safe_concat(title)
    end
  end
  title
end

#page_title_for_string(args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/geo_labels/application_helper.rb', line 92

def page_title_for_string(args)
  title = html_escape(args.first)
  options = args.last.is_a?(Hash) ? args.last : {}
  if back_options = options[:back]
    url = case back_options
          when Array then polymorphic_path(back_options)
          when true then :back
          else back_options
          end
    back_link = link_to (:i, nil, class: 'left arrow icon'), url, class: 'title-back-link'
    title = back_link.safe_concat(title)
  end
  title
end

#resource_title?(args) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'app/helpers/geo_labels/application_helper.rb', line 87

def resource_title?(args)
  args.first.is_a?(Symbol) &&
    (args[1].respond_to?(:model_name) || args[1].class.respond_to?(:model_name))
end

#search_result_info(records) ⇒ Object



130
131
132
133
134
135
# File 'app/helpers/geo_labels/application_helper.rb', line 130

def search_result_info(records)
  from_item = (records.current_page - 1) * records.limit_value + 1
  to_item = [from_item + records.size - 1, records.total_count].min
  from_item = 0 if records.total_count.zero?
  "#{from_item} - #{to_item} / #{records.total_count}"
end

#search_row(options = {}, &blk) ⇒ Object

Search helpers from dunlop



143
144
145
146
147
148
149
# File 'app/helpers/geo_labels/application_helper.rb', line 143

def search_row(options = {}, &blk)
  classes = Array.wrap(options[:class])
  classes |= ['search']
  classes << 'conditions-present' if @q.base.conditions.any?
  content = capture(&blk)
  (:tr, content, class: classes)
end


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/helpers/geo_labels/application_helper.rb', line 189

def table_destroy_link(record, path = nil, options = {})
  if options.has_key?(:authorized)
    return unless options[:authorized]
  else
    return unless can? :destroy, record
  end
  confirm_text = "Are you sure you want to delete #{record.class.model_name.human}"
  record_name = nil
  record_name = record.presentation_name if record.respond_to?(:presentation_name)
  record_name ||= record.name if record.respond_to?(:name)
  record_name ||= record.title if record.respond_to?(:title)
  confirm_text << " #{record_name}" if record_name.present?
  confirm_text << '?'
  link_to(
    (:i, nil, class: 'trash icon'),
    path || record,
    method: :delete,
    data: { confirm: confirm_text },
    class: 'table-link destroy ui mini negative icon button'
  )
end

This helper returns the link for showing a record inside a table



166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/geo_labels/application_helper.rb', line 166

def table_download_link(record, path = nil, options = {})
  if options.has_key?(:authorized)
    return unless options[:authorized]
  else
    return unless can? :download, record
  end
  link_to(
    (:i, nil, class: 'download icon'),
    path || [:download, record],
    class: 'table-link download ui mini violet icon button'
  )
end

This helper returns the link for editing a record inside a table



180
181
182
183
184
185
186
187
# File 'app/helpers/geo_labels/application_helper.rb', line 180

def table_edit_link(record, path = nil, options = {})
  if options.has_key?(:authorized)
    return unless options[:authorized]
  else
    return unless can? :update, record
  end
  link_to((:i, nil, class: 'write icon'), path || [:edit, record], class: 'table-link edit ui mini basic yellow icon button')
end

This helper returns the link for showing a record inside a table



152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/helpers/geo_labels/application_helper.rb', line 152

def table_show_link(record, path = nil, options = {})
  if options.has_key?(:authorized)
    return unless options[:authorized]
  else
    return unless can? :show, record
  end
  link_to(
    (:i, nil, class: 'folder open icon'),
    path || record,
    class: 'table-link show ui mini basic primary icon button'
  )
end