Module: Gluttonberg::Admin

Included in:
ApplicationHelper
Defined in:
app/helpers/gluttonberg/admin.rb,
app/controllers/gluttonberg/admin/help_controller.rb,
app/controllers/gluttonberg/admin/main_controller.rb,
app/controllers/gluttonberg/admin/content/flag_controller.rb,
app/controllers/gluttonberg/admin/content/main_controller.rb,
app/controllers/gluttonberg/admin/content/blogs_controller.rb,
app/controllers/gluttonberg/admin/content/pages_controller.rb,
app/controllers/gluttonberg/admin/settings/main_controller.rb,
app/controllers/gluttonberg/admin/user_sessions_controller.rb,
app/controllers/gluttonberg/admin/settings/users_controller.rb,
app/controllers/gluttonberg/admin/membership/main_controller.rb,
app/controllers/gluttonberg/admin/password_resets_controller.rb,
app/controllers/gluttonberg/admin/content/articles_controller.rb,
app/controllers/gluttonberg/admin/content/comments_controller.rb,
app/controllers/gluttonberg/admin/settings/locales_controller.rb,
app/controllers/gluttonberg/admin/content/galleries_controller.rb,
app/controllers/gluttonberg/admin/membership/groups_controller.rb,
app/controllers/gluttonberg/admin/membership/members_controller.rb,
app/controllers/gluttonberg/admin/asset_library/assets_controller.rb,
app/controllers/gluttonberg/admin/settings/stylesheets_controller.rb,
app/controllers/gluttonberg/admin/settings/global_history_controller.rb,
app/controllers/gluttonberg/admin/asset_library/collections_controller.rb,
app/controllers/gluttonberg/admin/settings/generic_settings_controller.rb,
app/controllers/gluttonberg/admin/content/page_localizations_controller.rb

Overview

Helpers specific to the administration interface. The majority are related to forms, but there are other short cuts for things like navigation.

Defined Under Namespace

Modules: AssetLibrary, Content, Membership, Settings Classes: BaseController, HelpController, MainController, PasswordResetsController, UserSessionsController

Instance Method Summary collapse

Instance Method Details

#admin_form(obj, opts = {}, &blk) ⇒ Object

A form helper which automatically injects the form builder which automatically wraps the fields in divs.



7
8
9
10
# File 'app/helpers/gluttonberg/admin.rb', line 7

def admin_form(obj, opts = {}, &blk)
  opts.merge!(:builder => Gluttonberg::Helpers::FormBuilder)
  form_for(obj, opts, &blk)
end

Writes out the back control for the sub nav.



142
143
144
# File 'app/helpers/gluttonberg/admin.rb', line 142

def back_link(name, url)
  (:li, link_to(name, url , :title => name), :id => "backLink")
end

#backend_logo(default_logo_image_path, html_opts = {}, thumbnail_type = :backend_logo) ⇒ Object



294
295
296
297
298
299
300
301
302
303
# File 'app/helpers/gluttonberg/admin.rb', line 294

def (default_logo_image_path , html_opts={}, thumbnail_type = :backend_logo)
   = Gluttonberg::Setting.get_setting("backend_logo")
  asset = Asset.find(:first , :conditions => { :id =>  } )
  unless asset.blank?
    path = thumbnail_type.blank? ? asset.url : asset.url_for(thumbnail_type)
    (:img , "" , html_opts.merge( :alt => asset.name , :src => path ) )
  else
    image_tag(default_logo_image_path)
  end
end

#block(label = nil, opts = {}, &blk) ⇒ Object

If it’s passed a label this method will return a fieldset, otherwise it will just return the contents wrapped in a block.



103
104
105
106
107
108
109
110
111
# File 'app/helpers/gluttonberg/admin.rb', line 103

def block(label = nil, opts = {}, &blk)
  if label
    field_set_tag(label) do
      (:fieldset, opts, &blk)
    end
  else
    (:fieldset, opts, &blk)
  end
end

#contextual_helpObject

Checks to see if there is a matching help page for this particular controller/action. If there is it renders a link to the help controller.



75
76
77
78
79
80
81
82
83
# File 'app/helpers/gluttonberg/admin.rb', line 75

def contextual_help
  if Help.help_available?(:controller => params[:controller], :page => params[:action])
    (
      :p,
      link_to("Help", admin_help_path(:module_and_controller => params[:controller], :page => params[:action]), :class => "button"),
      :id => "contextualHelp"
    )
  end
end

#current_domainObject



374
375
376
377
# File 'app/helpers/gluttonberg/admin.rb', line 374

def current_domain
  domain = "#{request.protocol}#{request.host_with_port}/"
  domain.strip
end

#custom_javascript_include_tagObject



254
255
256
257
258
# File 'app/helpers/gluttonberg/admin.rb', line 254

def custom_javascript_include_tag
  if Rails.configuration.custom_js_for_cms == true
    javascript_include_tag "custom"
  end
end


248
249
250
251
252
# File 'app/helpers/gluttonberg/admin.rb', line 248

def custom_stylesheet_link_tag
  if Rails.configuration.custom_css_for_cms == true
    stylesheet_link_tag "custom"
  end
end

#date_format(date_time) ⇒ Object



286
287
288
289
290
291
292
# File 'app/helpers/gluttonberg/admin.rb', line 286

def date_format(date_time)
  if date_time < 1.week.ago
    date_time.strftime("%d/%m/%Y")
  else
    time_ago_in_words(date_time)
  end
end

#form_controls(return_url, opts = {}) ⇒ Object

Controls for standard forms. Writes out a save button and a cancel link



114
115
116
117
# File 'app/helpers/gluttonberg/admin.rb', line 114

def form_controls(return_url , opts={})
  content = "#{submit_tag("Save" , :id => opts[:submit_id], :class => "btn btn-success").html_safe} #{link_to("Cancel".html_safe, return_url, :class => "btn")}"
  (:p, content.html_safe, :class => "controls")
end

#gb_editable_field(object, property, options = {}) ⇒ Object

Creates an editable span for the given property of the given object.

Options

:method

Specify the HTTP method to use: 'PUT' or 'POST'.

:name

The name attribute to be used when the form is posted.

:update_url

The URL to submit the form to. Defaults to url_for(object).



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/helpers/gluttonberg/admin.rb', line 328

def gb_editable_field(object, property, options={})

  name = "#{object.class.to_s.underscore}[#{property}]"
  value = object.send property
  update_url = options.delete(:update_url) || url_for(object)
  args = {:method => 'PUT', :name => name}.merge(options)
  %{
    <span class="editable" data-id="#{object.id}" data-name="#{name}">#{value}</span>
    <script type="text/javascript">
      (function( $ ){
        $(function(){
          var args = {data: function(value, settings) {
            // Unescape HTML
            var retval = value
              .replace(/&amp;/gi, '&')
              .replace(/&gt;/gi, '>')
              .replace(/&lt;/gi, '<')
              .replace(/&quot;/gi, "\\\"");
            return retval;
          },
             type      : 'text',
             height : '20px',
             cancel    : 'Cancel',
             submit    : 'OK',
             indicator : '#{image_tag('/assets/gb_spinner.gif')}'
          };
          $.extend(args, #{args.to_json});
          $(".editable[data-id='#{object.id}'][data-name='#{name}']").editable("#{update_url}", args);
        });
      })( jQuery );
    </script>
  }.html_safe
end

#gb_error_messages_for(model_object) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'app/helpers/gluttonberg/admin.rb', line 158

def gb_error_messages_for(model_object)
  if model_object.errors.any?
      lis = ""
      model_object.errors.full_messages.each do |msg|
        lis << (:li , msg)
      end
    ul = (:ul , lis.html_safe).html_safe
    heading = (:h4 , "Sorry there was an error" , :class => "alert-heading" )
    (:div , (heading.html_safe + ul.html_safe) , :class => "model-error alert alert-block alert-error")
  end
end

#get_localized_value(name) ⇒ Object



67
68
69
70
# File 'app/helpers/gluttonberg/admin.rb', line 67

def get_localized_value(name)
  @localized_model ||= current_form_context.instance_variable_get(:@obj)
  @localized_model.current_localization.send(name)
end

#honeypot_field_tagObject



280
281
282
283
284
# File 'app/helpers/gluttonberg/admin.rb', line 280

def honeypot_field_tag
  html = label_tag(Rails.configuration.honeypot_field_name , Rails.configuration.honeypot_field_name.humanize )
  html << text_field_tag( Rails.configuration.honeypot_field_name )
   :div , html , :class => Rails.configuration.honeypot_field_name , :style => "display:none"
end

#localization_fieldObject

Returns a hidden field which stores the localization param in the form.



63
64
65
# File 'app/helpers/gluttonberg/admin.rb', line 63

def localization_field
  hidden_field(:name => "localization", :value => params[:localization])
end

#localization_picker(url) ⇒ Object

Returns a form for selecting the localized version of a record you want to edit. TODO DO we need that



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/gluttonberg/admin.rb', line 15

def localization_picker(url)
  # Collect the locale/dialect pairs
  locales = ::Gluttonberg::Locale.all(:fields => [:id, :name])
  localizations = []
  locales.each do |locale|
      localizations << ["#{locale.id}", "#{locale.name}"]
  end
  # Output the form for picking the locale
  form(:action => url, :method => :get, :id => "select-localization") do
    output = ""
    output << select(:name => :localization, :collection => localizations, :label => "Select localization", :selected => params[:localization])
    output << button("Edit", :class => "buttonGrey")
  end
end

#localized_field_opts(name, set_value = true) ⇒ Object

Returns a hash of common options to be used in the localized versions of fields.



52
53
54
55
56
57
58
59
60
# File 'app/helpers/gluttonberg/admin.rb', line 52

def localized_field_opts(name, set_value = true)
  prefix = current_form_context.instance_variable_get(:@name)
  opts = {
    :name   => "#{prefix}[localized_attributes][#{name}]",
    :id     => "#{prefix}_localized_#{name}"
  }
  opts[:value] = get_localized_value(name) if set_value
  opts
end

#localized_text_area(name, opts = {}) ⇒ Object

Returns a text area with the name, id and values for the localized version of the specified attribute.



46
47
48
# File 'app/helpers/gluttonberg/admin.rb', line 46

def localized_text_area(name, opts = {})
  text_area(get_localized_value(name), localized_field_opts(name, false).merge!(opts))
end

#localized_text_field(name, opts = {}) ⇒ Object

Returns a text field with the name, id and values for the localized version of the specified attribute.



39
40
41
42
# File 'app/helpers/gluttonberg/admin.rb', line 39

def localized_text_field(name, opts = {})
  final_opts = localized_field_opts(name)
  text_field(final_opts.merge!(opts))
end

#main_nav_entry(text, mod, url = nil, opts = {}) ⇒ Object

Takes text and url and checks to see if the path specified matches the current url. This is so we can add a highlight.



148
149
150
151
152
153
154
155
156
# File 'app/helpers/gluttonberg/admin.rb', line 148

def main_nav_entry(text, mod, url = nil, opts = {})
  if url
    li_opts = {:id => "#{mod}Nav"}
    if( ( request.env["REQUEST_PATH"] && (request.env["REQUEST_PATH"].match(%r{/#{mod}}) || request.env["REQUEST_PATH"] == url) )  || (request.env["REQUEST_PATH"] && request.env["REQUEST_PATH"].include?("content") && request.env["REQUEST_PATH"] == url ) )
      li_opts[:class] = "current"
    end
    ("li", link_to(text, url, opts), li_opts)
  end
end

Writes out a link styled like a button. To be used in the sub nav only



135
136
137
138
139
# File 'app/helpers/gluttonberg/admin.rb', line 135

def nav_link(*args)
  class_names = "button"
  class_names = "#{class_names} #{args[2][:class]}" if args.length >= 3
  (:li, active_link_to(args[0] , args[1] , :title => args[0]), :class => class_names)
end

#page_localization_picker(page_localizations) ⇒ Object

Returns a form for selecting the localized version of a record you want to edit.



32
33
34
# File 'app/helpers/gluttonberg/admin.rb', line 32

def page_localization_picker(page_localizations)

end

#page_table_rows(pages, output = "", inset = 0, row = 0) ⇒ Object

Writes out a row for each page and then for each page’s children, iterating down through the heirarchy.



177
178
179
180
181
182
183
184
# File 'app/helpers/gluttonberg/admin.rb', line 177

def page_table_rows(pages, output = "", inset = 0 , row = 0)
  pages.each do |page|
    row += 1
    output << render( :partial => "gluttonberg/admin/content/pages/row", :locals => { :page => page, :inset => inset , :row => row })
    page_table_rows(page.children, output, inset + 1 , row)
  end
  output.html_safe
end

#publisable_dropdown(form, object) ⇒ Object



187
188
189
190
191
192
# File 'app/helpers/gluttonberg/admin.rb', line 187

def publisable_dropdown(form ,object)
  val = object.state
  val = "ready" if val.blank? || val == "not_ready"
  @@workflow_states = [  [ 'Draft' , 'ready' ] , ['Published' , "published" ] , [ "Archived" , 'archived' ]  ]
  form.select( :state, options_for_select(@@workflow_states , val)   )
end

#publish_message(object, versions) ⇒ Object

shows publish message if object’s currect version is published



195
196
197
198
199
200
201
202
203
204
205
# File 'app/helpers/gluttonberg/admin.rb', line 195

def publish_message(object , versions)
  content = msg = ""

  if versions.length > 1
    msg = (:a,  "Click here to see other versions" , :onclick => "$('#select-version').toggle();" , :href => "javascript:;"  , :title => "Click here to see other versions").html_safe
    msg = (:span , msg , :class => "view-versions").html_safe
  end

  content = (:div , "Updated on #{object.updated_at.to_s(:long)}    #{msg}".html_safe , :class => "unpublish_message") unless object.updated_at.blank?
  content.html_safe
end

#publishable_form_controls(return_url, object_name, is_published) ⇒ Object

Controls for publishable forms. Writes out a draft , publish/unpublish button and a cancel link



120
121
122
123
124
125
126
# File 'app/helpers/gluttonberg/admin.rb', line 120

def publishable_form_controls(return_url , object_name , is_published )
  content = hidden_field(:published , :value => false)
  content += "#{link_to("<strong>Cancel</strong>", return_url)}"
  content += " or #{submit_tag("draft")}"
  content += " or #{submit_tag("publish" , :onclick => "publish('#{object_name}_published')" )}"
  (:p, content, :class => "controls")
end

#render_flash_messagesObject



305
306
307
308
309
310
311
312
313
314
315
# File 'app/helpers/gluttonberg/admin.rb', line 305

def render_flash_messages
    html = ""
    ["notice", "warning", "error"].each do |type|
        unless flash[type.intern].nil?
            html << ("div", flash[type.intern].to_s.html_safe,
                :id => "alert alert-#{type}", :class => "flash").html_safe
        end
    end

    ("div", html.html_safe, :id => "flash").html_safe
end

#slug_donotmodify_valObject



370
371
372
# File 'app/helpers/gluttonberg/admin.rb', line 370

def slug_donotmodify_val
  action_name == "edit"  || action_name == "update"
end

#sortable_column(column, title = nil) ⇒ Object



362
363
364
365
366
367
368
# File 'app/helpers/gluttonberg/admin.rb', line 362

def sortable_column(column, title = nil)
  title ||= column.titleize
  css_class = column == sort_column ? "current #{sort_direction}" : nil
  direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
  new_params = params.merge(:sort => column, :direction => direction)
  link_to title, new_params, {:class => css_class}
end

Writes out a nicely styled subnav with an entry for each of the specified links.



130
131
132
# File 'app/helpers/gluttonberg/admin.rb', line 130

def sub_nav(&blk)
  (:ul, :id => "subnav", :class => "nav nav-pills", &blk)
end

#tab(label, url) ⇒ Object

For adding a tab to the tab bar. It will automatically mark the current tab by examining the request path.



93
94
95
96
97
98
99
# File 'app/helpers/gluttonberg/admin.rb', line 93

def tab(label, url)
  if request.env["REQUEST_PATH"] && request.env["REQUEST_PATH"].match(%r{^#{url}})
    (:li, link_to(label, url), :class => "current")
  else
    (:li, link_to(label, url))
  end
end

#tab_bar(&blk) ⇒ Object

TODO Do we need these? Generates a styled tab bar



87
88
89
# File 'app/helpers/gluttonberg/admin.rb', line 87

def tab_bar(&blk)
  (:ul, {:id => "tabBar"}, &blk)
end

#tags_string(tag_type) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/helpers/gluttonberg/admin.rb', line 266

def tags_string(tag_type)
  @themes = ActsAsTaggableOn::Tag.find_by_sql(%{select DISTINCT tags.id , tags.name
    from tags inner join taggings on tags.id = taggings.tag_id
    where context = '#{tag_type}'
  })
  output = ""
  @themes.each do |theme|
    output << "," unless output.blank?
    output << theme.name
  end
  output
end

#version_listing(versions, selected_version_num) ⇒ Object



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
# File 'app/helpers/gluttonberg/admin.rb', line 207

def version_listing(versions , selected_version_num)
  unless versions.blank?
    output = "<div class='historycontrols'>"
    selected = versions.last.version
    selected_version = versions.last
    collection = []
    versions.each do |version|
      link = version.version
      snippet = "Version #{version.version} - #{version.updated_at.to_s(:long)}  " unless version.updated_at.blank?
      if version.version.to_i == selected_version_num.to_i
        selected = link
        selected_version = version
      end
      collection << [snippet , link]
    end

    # Output the form for picking the version
    versions_html = "<ul class='dropdown-menu'>"
    collection.each do |c|
      versions_html << (:li , link_to(c[0] , "?version=#{c[1]}") , :class => "#{c[1].to_s == selected.to_s ? 'active' : '' }" )
    end
    versions_html << "</ul>"

    current_version = '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">'
    current_version += "Editing Version #{selected_version.version} "
    current_version += '<span class="caret"></span>'
    current_version += '</a>'

    combined_versions = current_version
    combined_versions += versions_html

    output << (:div , combined_versions.html_safe, :class => "btn-group" )

    output += "</div>"
    output += "<div class='clear'></div>"
    output += "<br />"
    output += "<br />"
    output.html_safe
  end
end

#website_titleObject



170
171
172
173
# File 'app/helpers/gluttonberg/admin.rb', line 170

def website_title
  title = Gluttonberg::Setting.get_setting("title")
  (title.blank?)? "Gluttonberg" : title.html_safe
end


260
261
262
263
264
# File 'app/helpers/gluttonberg/admin.rb', line 260

def wysiwyg_js_css_link_tag
  if Gluttonberg::Setting.get_setting("enable_WYSIWYG") == "Yes"
    javascript_include_tag("/assets/tiny_mce/jquery.tinymce.js")
  end
end