Module: ComponentHelper

Defined in:
app/helpers/component_helper.rb

Instance Method Summary collapse

Instance Method Details

#button_delete(obj, opts = {}) ⇒ Object



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

def button_delete(obj, opts = {})
  opts[:scope_params] ||= {}
  ( link_to icon('trash'), {controller: obj.class.name.tableize, id: obj, action: :destroy}.merge(opts[:scope_params]), confirm: 'Apakah Anda yakin?', method: :delete, remote: true, title: 'Hapus data', rel: 'tooltip', class: 'btn btn-mini btn-danger delete' ).html_safe
end

#button_delete_with_cancan(obj, opts = {}) ⇒ Object



100
101
102
# File 'app/helpers/component_helper.rb', line 100

def button_delete_with_cancan(obj, opts = {})
  can?(:destroy, obj) ? button_delete(obj, opts).html_safe : ''
end

#button_edit(obj, opts = {}) ⇒ Object



82
83
84
85
# File 'app/helpers/component_helper.rb', line 82

def button_edit(obj, opts = {})
  opts[:scope_params] ||= {}
  ( link_to icon('pencil'), {controller: obj.class.name.tableize, id: obj, action: :edit}.merge(opts[:scope_params]), remote: true, title: 'Ubah data', rel: 'tooltip', class: 'btn btn-mini edit' ).html_safe
end

#button_edit_delete(obj) ⇒ Object



92
93
94
# File 'app/helpers/component_helper.rb', line 92

def button_edit_delete(obj)
  [button_edit(obj), button_delete(obj)].join(' ').strip.html_safe
end

#button_edit_delete_with_cancan(obj, opts = {}) ⇒ Object



104
105
106
# File 'app/helpers/component_helper.rb', line 104

def button_edit_delete_with_cancan(obj, opts = {})
  [button_edit_with_cancan(obj, opts), button_delete_with_cancan(obj, opts)].join(' ').strip.html_safe
end

#button_edit_with_cancan(obj, opts = {}) ⇒ Object



96
97
98
# File 'app/helpers/component_helper.rb', line 96

def button_edit_with_cancan(obj, opts = {})
  can?(:update, obj) ? button_edit(obj, opts).html_safe : ''
end

#button_form_actions(fobj) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/helpers/component_helper.rb', line 71

def button_form_actions(fobj)
  result = ''
  # result << (fobj.error :base).presence || ''
  result << '<div class="form-actions">'
  result << (fobj.button :submit, 'Simpan', class: 'btn btn-large btn-primary btn_submit')
  result << '</div>'
  result.html_safe
end

#code_with_name(code, name, row = 1) ⇒ Object



108
109
110
111
112
113
114
# File 'app/helpers/component_helper.rb', line 108

def code_with_name(code, name, row = 1)
  if row == 2
    "<strong>#{code}</strong><br/>#{name}".html_safe
  else
    "<strong>#{code}:</strong> #{name}".html_safe
  end
end

#component_by_page(slug) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/component_helper.rb', line 144

def component_by_page slug
  comp_page = Page.find_by_slug slug
  result = ''
  if comp_page
    result =
      "<div class='component'>" +
      "  <div class='component_title'>" +
      "    <h2> #{comp_page.name}" +
      "    </h2>" +
      "  </div>" +
      "  <div class='component_content'>#{truncate_html(comp_page.description, 300)}" +
      "  </div>" +
      "</div>"
  end
  result.html_safe
end

#error_message_list(errors) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'app/helpers/component_helper.rb', line 240

def error_message_list errors
  result = ""
  if errors.present?
    result += ' <div class="alert alert-error">'
    result += '   <ul>'
    errors.each do |k, v|
      result += '     <li>'
      result += t(k.to_s.split('.').join('_')) + ' ' + v
      result += '     </li>'
    end
    result += '   </ul>'
    result += ' </div>'
  end
  result.html_safe
end

#fake_pagination(rtc_controller_name) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/helpers/component_helper.rb', line 220

def fake_pagination(rtc_controller_name)
  per_page_options = [5, 10, 20, 50, 100, 200]
  result = '<div class="pagination">'
  result += '<span class="first_page disabled">&lt;&lt;</span>'
  result += '<span class="previous_page disabled">&lt;</span>'
  result += '<div class="current_page_part">Hal. 1 dari 1'
  result += '</div>'
  result += '<span class="previous_page disabled">&lt;</span>'
  result += '<span class="last_page disabled">&gt;&gt;</span>'
  result += '<div class="per_page_part">'
  result += (select_tag 'per_page', (per_page_options.map{|m| "<option #{(m==params[:per_page].to_i ? 'selected=selected' : '')}>#{m}</option>"}.join.html_safe))
  result += 'Per hal. </div>'
  result += '<div class="go_to_page_part">'
  result += '<input type="text" value="1" name="page">'
  result += (link_to "Go", {controller: rtc_controller_name}, remote: true, class: "gotopage btn-mini")
  result += '</div>'
  result += '</div>'
  result.html_safe
end

#icon(ico, text = '', pos = 'left') ⇒ Object



61
62
63
64
65
66
67
# File 'app/helpers/component_helper.rb', line 61

def icon(ico, text = '', pos = 'left')
  if pos.eql? 'right'
    sanitize text + ' <i class="icon-' + ico + '"></i>'
  else
    sanitize '<i class="icon-' + ico + '"></i> ' + text
  end
end

#lvr_row_class(item) ⇒ Object



140
141
142
# File 'app/helpers/component_helper.rb', line 140

def lvr_row_class(item)
  "#{controller_name}_#{item.class.name.tableize.singularize}_list_view_row"
end

#lvr_row_id(item) ⇒ Object



136
137
138
# File 'app/helpers/component_helper.rb', line 136

def lvr_row_id(item)
  "#{item.class.name.tableize.singularize}_list_view_row_#{item.id}"
end

#mapping_label(arr) ⇒ Object

Needed for constructing dot format in recapitulation matrix



172
173
174
# File 'app/helpers/component_helper.rb', line 172

def mapping_label(arr)
  [['mapping'] + arr].flatten.join('.')
end

#part(opts = {}) ⇒ Object

Return part div



48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/component_helper.rb', line 48

def part(opts = {})
   :div, class: "part #{opts[:class]}" do
    if opts[:label].present?
      (:span, opts[:label], class: "part_label") + 
      (:span, opts[:value], class: "part_value", title: opts[:title], rel: 'tooltip')        
    else
      (:span, opts[:value], class: "part_value", title: opts[:title], rel: 'tooltip')
    end
  end
end

#render_recapitulation(options) ⇒ Object



184
185
186
# File 'app/helpers/component_helper.rb', line 184

def render_recapitulation(options)
  render partial: 'rich_table_component/rtc/recapitulation_matrix', locals: options
end

#render_rich_table_component(options) ⇒ Object



180
181
182
# File 'app/helpers/component_helper.rb', line 180

def render_rich_table_component(options)
  render partial: 'rich_table_component/rtc/component', locals: options
end

#render_rtc(options) ⇒ Object



176
177
178
# File 'app/helpers/component_helper.rb', line 176

def render_rtc(options)
  render partial: 'rich_table_component/rtc/component', locals: options
end

#row_number(local_var_assignments) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/helpers/component_helper.rb', line 200

def row_number local_var_assignments
  result = '<td class="numeric rtc_row_number">'
  
  if local_var_assignments[:rtc_partial].present?
    counter = local_var_assignments["#{local_var_assignments[:rtc_partial].split('/').last}_counter".to_sym].presence || 0
    offset = local_var_assignments[:offset]
    if offset
      result << "#{counter + offset + 1}."
    else
      result << '(baru)'
    end
  else
      result << '(update)'
  end  
  result << '</td>'

  result.html_safe
end

#row_selection(local_var_assignments, options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'app/helpers/component_helper.rb', line 188

def row_selection local_var_assignments, options = {}
  obj = local_var_assignments["#{local_var_assignments[:rtc_partial]}".to_sym]
  options[:visible] = true if options[:visible].nil?
  result = '<td class="numeric rtc_row_select_cell">'
  if options[:visible]
    result << (check_box_tag "#{obj.class.name.tableize}[]", obj.id, false, class: 'rtc_row_select', style: 'height: 0px; margin-top: 0px; margin-right: 7px;')
  end
  result << '</td>'

  result.html_safe
end

rendering link for sortable column that used by rich table component



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/component_helper.rb', line 5

def sort_link(column = {}, options = {})
  new_params = Hash.new
  new_params.merge! params
  case column
  when Hash
    new_params.merge!(column[:params]) if column[:params].present?
    options[:title] ||= column[:title].to_s

    if column[:value].present?
      options[:title] = column[:label]
      column = column[:value]
    else
      column = column[:column]
    end
  when Array
    options[:title] = column[1].presence || column.first.to_s.split('.').last(2).join('_')
    column = column.first
  end

  options[:title] = options[:title].presence || column.to_s.split('.').last(2).join('_')

  options[:controller] ||= controller_name
  
  options[:params][:rtc_controller_name] = options[:rtc_controller_name].presence || controller_name
  options[:params][:rtc_partial] = options[:rtc_partial] if options[:rtc_partial].present?
  
  css_class = (column == sort_column) ? "current #{sort_direction}" : nil  
  direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"  
  options[:params].stringify_keys!
  new_params.merge!(options[:params])
  if !action_name.eql? 'index'
    new_params.merge!({pgos: true})
  end
  new_params.merge!({controller: options[:controller], sort: column, direction: direction, page: params[:page]}.stringify_keys!)
  #new_params.merge!({controller: options[:controller], sort: column, direction: direction, page: params[:page]})
  
  link_to t("#{options[:title]}"), new_params, {class: css_class, remote: true, title: t("#{options[:title]}")}  
end

#time_period(sdate, edate) ⇒ Object



165
166
167
168
169
# File 'app/helpers/component_helper.rb', line 165

def time_period(sdate, edate)
  strsdate = sdate.present? ? (mdate sdate) : "..."
  stredate = edate.present? ? (mdate edate) : "..."
  "#{strsdate} - #{stredate}"
end

#uneditable_input_control(value, label = ' ') ⇒ Object



116
117
118
119
120
121
122
123
# File 'app/helpers/component_helper.rb', line 116

def uneditable_input_control(value, label = ' ')
   :div, class: "control-group" do
    (:label, label, class: "control-label") +
    (:div, class: "controls") do
      (:div, value, class: "uneditable-input-plain") 
    end
  end
end

#uneditable_textarea_control(value, label = ' ') ⇒ Object



126
127
128
129
130
131
132
133
# File 'app/helpers/component_helper.rb', line 126

def uneditable_textarea_control(value, label = ' ')
   :div, class: "control-group" do
    (:label, label, class: "control-label") +
    (:div, class: "controls") do
      (:div, value, class: "uneditable-textarea-plain") 
    end
  end
end

#with_html_title(text) ⇒ Object



161
162
163
# File 'app/helpers/component_helper.rb', line 161

def with_html_title text
  "<span title='#{text}'>#{text}</span>".html_safe if text.present?
end