Module: Cambium::CambiumHelper

Included in:
AdminController
Defined in:
app/helpers/cambium/cambium_helper.rb

Instance Method Summary collapse

Instance Method Details

#adminObject



16
17
18
# File 'app/helpers/cambium/cambium_helper.rb', line 16

def admin
  @admin ||= Cambium::AdminPresenter.new(self)
end

#admin_formObject



28
29
30
31
32
33
34
35
36
# File 'app/helpers/cambium/cambium_helper.rb', line 28

def admin_form
  @admin_form ||= begin
    if action_name == 'new' || action_name == 'create'
      admin_view.form.new
    else
      admin_view.form.edit
    end
  end
end

#admin_modelObject



42
43
44
# File 'app/helpers/cambium/cambium_helper.rb', line 42

def admin_model
  @admin_model ||= admin_view.model.constantize
end

#admin_routesObject



38
39
40
# File 'app/helpers/cambium/cambium_helper.rb', line 38

def admin_routes
  @admin_routes ||= admin.routes(@object)
end

#admin_tableObject



24
25
26
# File 'app/helpers/cambium/cambium_helper.rb', line 24

def admin_table
  @admin_table ||= admin_view.nil? ? nil : admin_view.table
end

#admin_viewObject



20
21
22
# File 'app/helpers/cambium/cambium_helper.rb', line 20

def admin_view
  @admin_view ||= admin.view(controller_name)
end

#avatar(user, size = 100, klass = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'app/helpers/cambium/cambium_helper.rb', line 8

def avatar(user, size = 100, klass = nil)
  gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
  (:div, :class => "avatar-container #{klass}") do
    image_tag "http://gravatar.com/avatar/#{gravatar_id}.png?s=#{size}&d=mm",
      :class => 'avatar'
  end
end

#cambium_form(obj, fields, url = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/cambium/cambium_helper.rb', line 117

def cambium_form(obj, fields, url=nil)
  (:section, :class => 'form') do
    if url.nil?
      case action_name
      when 'edit', 'update'
        url = cambium_route(:show, obj)
      else
        url = cambium_route(:index, obj)
      end
    end
    simple_form_for obj, :url => url do |f|
      cambium_form_fields(f, obj, fields)
    end
  end
end

#cambium_form_fields(f, obj, fields) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'app/helpers/cambium/cambium_helper.rb', line 133

def cambium_form_fields(f, obj, fields)
  o = ''
  fields.to_h.each do |data|
    attr = data.first.to_s
    options = data.last
    readonly = options.readonly || false
    label = options.label || attr.titleize
    if options.type == 'heading'
      o += (:h2, options.label || attr.titleize)
    elsif ['select','check_boxes','radio_buttons'].include?(options.type)
      parts = options.options.split('.')
      if parts.size > 1
        collection = parts[0].constantize.send(parts[1])
      else
        collection = options.options
      end
      o += f.input(
        attr.to_sym,
        :as => options.type,
        :collection => collection,
        :label => label,
        :readonly => readonly
      )
    elsif ['date','time'].include?(options.type)
      if obj.send(attr).present?
        val = (options.type == 'date') ?
          obj.send(attr).strftime("%d %B, %Y") :
          obj.send(attr).strftime("%l:%M %p")
      end
      o += f.input(
        attr.to_sym,
        :as => :string,
        :label => label,
        :input_html => {
          :class => "picka#{options.type}",
          :value => val.nil? ? nil : val
        },
        :readonly => readonly
      )
    elsif options.type == 'datetime'
      o += (:div, :class => 'input string pickadatetime') do
        o2 = (:label, attr.to_s.humanize.titleize)
        o2 += (
          :input,
          '',
          :label => label,
          :placeholder => 'Date',
          :type => 'text',
          :class => 'pickadatetime-date',
          :value => obj.send(attr).present? ?
            obj.send(attr).strftime("%d %B, %Y") : '',
          :readonly => readonly
        )
        o2 += (
          :input,
          '',
          :label => label,
          :placeholder => 'Time',
          :type => 'text',
          :class => 'pickadatetime-time',
          :value => obj.send(attr).present? ?
            obj.send(attr).strftime("%l:%M %p") : '',
          :readonly => readonly
        )
        o2 += f.input(
          attr.to_sym,
          :as => :hidden,
          :wrapper => false,
          :label => false,
          :input_html => { :class => 'pickadatetime' }
        )
      end
    elsif options.type == 'markdown'
      o += (:div, :class => "input text optional #{attr}") do
        o2  = (:label, label, :for => attr)
        o2 += (
          :div,
          f.markdown(attr.to_sym),
          :class => 'markdown'
        )
      end
    elsif options.type == 'file'
      o += f.input(
        attr.to_sym,
        :as => options.type,
        :label => label,
        :readonly => readonly
      )
      o += link_to(
        obj.send(attr).name,
        obj.send(attr).url,
        :class => 'file',
        :target => :blank
      ) unless obj.send(attr).blank?
    else
      o += f.input(
        attr.to_sym,
        :as => options.type,
        :label => label,
        :readonly => readonly
      )
    end
  end
  o += f.submit
  o.html_safe
end

#cambium_page_title(title) ⇒ Object



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
# File 'app/helpers/cambium/cambium_helper.rb', line 46

def cambium_page_title(title)
  (:div, :id => 'title-bar') do
    o  = (:h2, title, :class => 'page-title')
    if is_index? && has_new_form?
      o += link_to(
        admin_view.form.new.title,
        admin_routes.new,
        :class => 'button new'
      )
    end
    if is_index? && admin_view.export.present?
      o += link_to(
        admin_view.export.button || "Export #{admin_table.title}",
        "#{admin_routes.index}.csv",
        :class => 'button export'
      )
    end
    if is_edit? && can_delete?
      o += link_to(
        admin_view.form.buttons.delete,
        admin_routes.delete,
        :class => 'button delete',
        :method => :delete,
        :data => { :confirm => 'Are you sure?' }
      )
    end
    o.html_safe
  end
end

#cambium_route(action, obj = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/helpers/cambium/cambium_helper.rb', line 240

def cambium_route(action, obj = nil)
  case action
  when :index
    begin
      main_app
        .polymorphic_path [:admin, obj.class.to_s.tableize.pluralize.to_sym]
    rescue
      cambium.polymorphic_path [:admin, obj.class.to_s.tableize.pluralize.to_sym]
    end
  when :edit
    begin
      main_app.polymorphic_path [:edit, :admin, obj]
    rescue
      cambium.polymorphic_path [:edit, :admin, obj]
    end
  else
    begin
      main_app.polymorphic_path [:admin, obj]
    rescue
      cambium.polymorphic_path [:admin, obj]
    end
  end
end

#cambium_table(collection, columns) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/cambium/cambium_helper.rb', line 76

def cambium_table(collection, columns)
  obj_methods = []
  (:section, :class => 'data-table') do
    p = (:table) do
      o = (:thead) do
        (:tr) do
          o2 = ''
          columns.to_h.each do |col|
            obj_methods << col.first.to_s
            o2 += (:th, col.last.heading)
          end
          o2 += (:th, nil)
          o2.html_safe
        end
      end
      o += (:tbody) do
        o2 = ''
        collection.each do |obj|
          o2 += (:tr) do
            o3 = ''
            obj_methods.each do |method|
              o3 += (:td, obj.send(method))
            end
            path = "edit_admin_#{controller_name.singularize}_path"
            begin
              route = cambium.send(path, obj)
            rescue
              route = main_app.send(path, obj)
            end
            o3 += (:td, link_to('', route), :class => 'actions')
            o3.html_safe
          end
        end
        o2.html_safe
      end
      o.html_safe
    end
    p += paginate(collection)
  end
end

#can_delete?Boolean

Returns:

  • (Boolean)


276
277
278
279
# File 'app/helpers/cambium/cambium_helper.rb', line 276

def can_delete?
  admin_view.form.present? && admin_view.form.buttons.present? &&
    admin_view.form.buttons.delete.present?
end

#has_new_form?Boolean

Returns:

  • (Boolean)


272
273
274
# File 'app/helpers/cambium/cambium_helper.rb', line 272

def has_new_form?
  admin_view.form.present? && admin_view.form.new.present?
end

#is_edit?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'app/helpers/cambium/cambium_helper.rb', line 268

def is_edit?
  ['edit','update'].include?(action_name)
end

#is_index?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'app/helpers/cambium/cambium_helper.rb', line 264

def is_index?
  action_name == 'index'
end

#not_foundObject

Raises:

  • (ActionController::RoutingError)


4
5
6
# File 'app/helpers/cambium/cambium_helper.rb', line 4

def not_found
  raise ActionController::RoutingError.new('Not Found')
end