Module: Gluttonberg::Admin::Form

Included in:
Gluttonberg::Admin
Defined in:
app/helpers/gluttonberg/admin/form.rb

Instance Method Summary collapse

Instance Method Details

#admin_form_controls_for_approving_or_decling_objects(version, opts = {}) ⇒ Object

Form controls for admin when they are viewing submitted revision of an object. It renders “Approve”, “Decline” buttons.



54
55
56
57
58
# File 'app/helpers/gluttonberg/admin/form.rb', line 54

def admin_form_controls_for_approving_or_decling_objects(version, opts={})
  html = submit_tag("Approve" , :id => "publish_btn", :class => "btn btn-success publishing_btn").html_safe
  html += " ".html_safe +  link_to("Decline", admin_decline_content_path(version.class.name.gsub("::Version",""), version.id) , :id => "decline_btn", :class => "btn btn-danger ").html_safe
  (:p, html.html_safe, :class => "controls")
end

#admin_form_controls_for_draft_objects(opts = {}) ⇒ Object

Form controls for admin when object is on draft status. It renders “Save draft”, “Publish” buttons.



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

def admin_form_controls_for_draft_objects(opts={})
  html = submit_tag("Save draft" , :id => "draft_btn", :class => "btn publishing_btn").html_safe
  html += " ".html_safe +  submit_tag("Publish" , :id => "publish_btn", :class => "btn btn-success publishing_btn").html_safe
  (:p, html.html_safe, :class => "controls")
end

#admin_form_controls_for_published_objects(revisions = true, opts = {}) ⇒ Object

Form controls for admin when object is already published. It rendres “Save revision”, “Update”, “Unpublish” buttons.



45
46
47
48
49
50
51
# File 'app/helpers/gluttonberg/admin/form.rb', line 45

def admin_form_controls_for_published_objects(revisions=true, opts={})
  html =  ""
  html += submit_tag("Save revision" , :id => "revision_btn", :class => "btn publishing_btn").html_safe if revisions == true
  html += " ".html_safe +  submit_tag("Update" , :id => "update_btn", :class => "btn btn-success publishing_btn").html_safe
  html += " ".html_safe +  submit_tag("Unpublish" , :id => "unpublish_btn", :class => "btn btn-danger publishing_btn").html_safe
  (:p, html.html_safe, :class => "controls")
end

#contributor_form_controls(published, opts = {}) ⇒ Object

Form controls for contributor. It renders “Save draft”, “Submit for approval” buttons.



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

def contributor_form_controls(published, opts={})
  html = submit_tag("Save draft" , :id => "#{published ? 'revision_btn' : 'draft_btn'}", :class => "btn publishing_btn").html_safe
  html += " ".html_safe +  submit_tag("Submit for approval" , :id => "approval_btn", :class => "btn btn-success publishing_btn").html_safe
  (:p, html.html_safe, :class => "controls")
end

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

Controls for standard forms. Writes out a save button with gluttonberg default styling



14
15
16
17
# File 'app/helpers/gluttonberg/admin/form.rb', line 14

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).



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/gluttonberg/admin/form.rb', line 123

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

#honeypot_field_tagObject

Use this helper on public submissions forms to avoid robotic submissions



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

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

#publish_message(object, versions) ⇒ Object

shows publish message if object’s currect version is published



20
21
22
23
24
25
26
27
28
# File 'app/helpers/gluttonberg/admin/form.rb', line 20

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

#submit_and_publish_controls(form, object, can_publish, schedule_field = true, revisions = true, opts = {}) ⇒ Object

new form controls based on new logic of authorization and publishing workflow It renders some hidden fields which manage publishing status. It also renders form controls based on current_user role, and status of object.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/gluttonberg/admin/form.rb', line 63

def submit_and_publish_controls(form, object, can_publish, schedule_field=true, revisions=true, opts={})
  version_status = ''
  begin
    version_status = !object.respond_to?(:loaded_version) || object.loaded_version.blank? ? '' : object.loaded_version.version_status
  rescue
  end
  html = ("legend", "Publish").html_safe
  if object.published?
    html += (:p, "<span class='date'>Published on #{object.published_at.strftime("%d/%m/%Y")}</span>".html_safe)
  else
    html += form.publishing_schedule(schedule_field)
  end
  html += form.hidden_field(:state, :class => "_publish_state") 
  html += form.hidden_field(:_publish_status, :class => "_publish_status") 
  html += if can_publish
    if version_status == 'submitted_for_approval'
      admin_form_controls_for_approving_or_decling_objects(object.loaded_version, opts)
    elsif object.published?
      admin_form_controls_for_published_objects(revisions, opts)
    else
      admin_form_controls_for_draft_objects(opts)
    end
  else
    contributor_form_controls(object.published?, opts)
  end
  html.html_safe
end

#version_listing(versions, selected_version_num) ⇒ Object

It renders a dropdown with the list of all revisions of given object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/gluttonberg/admin/form.rb', line 92

def version_listing(versions , selected_version_num)
  unless versions.blank?
    versions = versions.order("version DESC")
    selected = versions.last.version
    selected_version = versions.first
    versions.each do |version|
      if version.version.to_i == selected_version_num.to_i
        selected = version.version
        selected_version = version
      end
    end
    render :partial => "/gluttonberg/admin/shared/version_listing", :locals => {
      :versions => versions,
      :selected_version_num => selected_version_num,
      :selected => selected,
      :selected_version => selected_version
    }
  end
end