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

def admin_model
  @admin_model ||= begin
    admin_view.model.constantize
  rescue
    "Cambium::#{admin_view.model}".constantize
  end
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_field(f, obj, field) ⇒ Object



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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/helpers/cambium/cambium_helper.rb', line 163

def cambium_field(f, obj, field)
  attr = field.first.to_s
  options = field.is_a?(OpenStruct) ? field : field.last
  options = options.to_ostruct unless options.class == OpenStruct
  readonly = options.readonly || false
  label = options.label || attr.titleize
  required = options.required || false
  if options.type == 'heading'
    (: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
    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
    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'
    (:div, :class => 'input string pickadatetime') do
      o2 = (:label, label)
      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'
    (: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 == 'wysiwyg'
    f.input(attr.to_sym, :as => :text, :label => label,
            :input_html => { :class => 'editor' }, :required => required)
  elsif options.type == 'media'
    (:div, :class => 'input media-picker file') do
      o2  = (:label, label)
      o2 += link_to('Choose File', '#', :class => 'add')
      o2 += link_to('Remove File', '#',
        :class => "remove #{'active' unless obj.send(attr).blank? }")
      unless obj.send(attr).blank?
        ext = obj.send(attr).upload.ext.downcase
        if ['jpg','jpeg','gif','png'].include?(ext)
          o2 += image_tag(obj.send(attr).image_url(400, 400))
        end
        o2 += link_to(obj.send(attr).upload.name,
                      obj.send(attr).upload.url,
                     :class => 'file', :target => :blank)
      end
      o2 += f.input(attr.to_sym, :as => :hidden, :wrapper => false)
    end
  elsif options.type == 'file'
    o = f.input(attr.to_sym, :as => options.type, :label => label,
            :readonly => readonly, :required => required)
    unless obj.send(attr).blank?
      # Dragonfly ...
      if obj.send(attr).respond_to?(:ext)
        if %w(jpg jpeg gif png).include?(obj.send(attr).ext.downcase)
          o += image_tag obj.send(attr)
                            .thumb("200x200##{obj.send("#{attr}_gravity")}")
                            .url
          o += (:div, :class => 'image-actions') do
            o2  = ''.html_safe
            o2 += link_to('Crop Image', '#', :class => 'crop',
                          :target => :blank, :data => {
                          :url => obj.send(attr).url,
                          :width => obj.send(attr).width,
                          :height => obj.send(attr).height }) if options.crop
            o2 += link_to(obj.send(attr).name, obj.send(attr).url,
                     :class => 'file', :target => :blank)
            o2 += f.input :"#{attr}_gravity", :as => :hidden
          end
        else
          o += link_to(obj.send(attr).name, obj.send(attr).url,
                       :class => 'file', :target => :blank)
        end
      # CarrierWave (assumed, for now)
      else
        if %w(jpg jpeg gif png).include?(obj.send(attr).file.extension.downcase)
          o += image_tag obj.send(attr).thumb.url
          o += (:div, :class => 'image-actions') do
            o2  = ''.html_safe
            # o2 += link_to('Crop Image', '#', :class => 'crop',
            #               :target => :blank, :data => {
            #               :url => obj.send(attr).url,
            #               :width => obj.send(attr).width,
            #               :height => obj.send(attr).height }) if options.crop
            o2 += link_to(obj.send(attr).file.filename, obj.send(attr).url,
                     :class => 'file', :target => :blank)
            # o2 += f.input :"#{attr}_gravity", :as => :hidden
          end
        else
          o += link_to(obj.send(attr).name, obj.send(attr).url,
                       :class => 'file', :target => :blank)
        end
      end
    end
    o
  else
    f.input(attr.to_sym, :as => options.type, :label => label,
            :readonly => readonly, :required => required)
  end
end

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/helpers/cambium/cambium_helper.rb', line 136

def cambium_form(obj, fields, url=nil, &block)
  (: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|
      o  = cambium_form_fields(f, obj, fields)
      o += capture(f, &block) if block_given?
      o += f.submit
      o
    end
  end
end

#cambium_form_fields(f, obj, fields) ⇒ Object



155
156
157
158
159
160
161
# File 'app/helpers/cambium/cambium_helper.rb', line 155

def cambium_form_fields(f, obj, fields)
  o = ''
  fields.to_h.each do |field|
    o += cambium_field(f, obj, field)
  end
  o.html_safe
end

#cambium_page_title(title) ⇒ Object



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

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,
        cambium_route(:new),
        :class => 'button new'
      )
    end
    if is_index? && admin_view.export.present?
      o += link_to(
        admin_view.export.button || "Export #{admin_table.title}",
        "#{cambium_route(:index)}.csv",
        :class => 'button export'
      )
    end
    if is_edit? && can_delete?
      o += link_to(
        admin_view.form.buttons.delete,
        cambium_route(:delete, @object),
        :class => 'button delete',
        :method => :delete,
        :data => { :confirm => 'Are you sure?' }
      )
    end
    o.html_safe
  end
end

#cambium_route(action, obj = nil) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/helpers/cambium/cambium_helper.rb', line 287

def cambium_route(action, obj = nil)
  c_name = controller_name.singularize
  case action
  when :index
    begin
      main_app
        .polymorphic_path [:admin, controller_name.to_sym]
    rescue
      cambium.polymorphic_path [:admin, controller_name.to_sym]
    end
  when :edit
    begin
      main_app.polymorphic_path [:edit, :admin, obj]
    rescue
      cambium.polymorphic_path [:edit, :admin, obj]
    end
  when :new
    begin
      main_app.polymorphic_path [:new, :admin, c_name.to_sym]
    rescue
      cambium.polymorphic_path [:new, :admin, c_name.to_sym]
    end
  else
    begin
      main_app.polymorphic_path [:admin, obj]
    rescue
      cambium.polymorphic_path [:admin, obj]
    end
  end
end

#cambium_table(collection, columns) ⇒ Object



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/cambium/cambium_helper.rb', line 80

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.last.display_method || col.first.to_s)
            if col.last.sortable
              o2 += (:th) do
                path = "admin_#{controller_name}_path"
                args = {
                  :page => params[:page] || 1,
                  :sort_by => col.first,
                  :order => (params[:order] == 'asc' &&
                             params[:sort_by] == col.first) ? :desc : :asc
                }
                begin
                  route = cambium.send(path, args)
                rescue
                  route = main_app.send(path, args)
                end
                klass  = args[:order].to_s
                klass += ' active' if params[:sort_by] == col.first.to_s
                link_to(col.last.heading, route, :class => klass)
              end
            else
              o2 += (:th, col.last.heading)
            end
          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
            o3 += (:td, link_to('', cambium_route(:edit, obj)),
                              :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)


330
331
332
333
# File 'app/helpers/cambium/cambium_helper.rb', line 330

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)


326
327
328
# File 'app/helpers/cambium/cambium_helper.rb', line 326

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

#is_edit?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'app/helpers/cambium/cambium_helper.rb', line 322

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

#is_index?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'app/helpers/cambium/cambium_helper.rb', line 318

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