Module: Atreides::ApplicationHelper

Includes:
Extendable
Defined in:
app/helpers/atreides/application_helper.rb

Overview

Methods added to this helper will be available to all templates in the application.

Instance Method Summary collapse

Instance Method Details



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/helpers/atreides/application_helper.rb', line 186

def action_link(action, record, record_name, final_options = {})
  url = case action.to_sym
  when :show, :destroy, :update
    "#{record_name}_path"
  when :edit
    "edit_#{record_name}_path"
  when :new
    "new_#{record_name}_path"
  else
    "#{action}_#{record_name}_path"
  end

  options = case action.to_sym
  when :destroy
    {:confirm => 'Are you sure?', :method => :delete}
  when :moderate
    {:confirm => 'Are you sure?', :method => :put }
  end
  options ||= {}

  link_to action.capitalize, send(url, record), {:class => action}.merge(options).merge(final_options)
end

#body_classesObject



93
94
95
96
97
98
# File 'app/helpers/atreides/application_helper.rb', line 93

def body_classes
  con = params[:controller].split('/').last.strip
  act = params[:action].strip
  id  = resource ? resource.to_param : nil
  "#{con} #{con}-#{act} #{id} #{@body_classes.to_s}"
end

#col_to_class_name(col) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/helpers/atreides/application_helper.rb', line 163

def col_to_class_name(col)
  case col.type
  when :text
    :textarea
  when :boolean
    :checkbox
  when :date, :datetime, :time
    :select
  else
    :text
  end
end

#col_to_field(col) ⇒ Object

Forms and Lists



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/helpers/atreides/application_helper.rb', line 148

def col_to_field(col)
  case col.type
  when :text
    :text_area
  when :boolean
    :check_box
  when :date
    :date_select
  when :datetime, :time
    :datetime_select
  else
    :text_field
  end
end

#facebook_meta_tagsObject



4
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
43
44
# File 'app/helpers/atreides/application_helper.rb', line 4

def facebook_meta_tags

  return unless page_object

  title = Settings.app_name + " - #{page_title}"

  str  = tag(:meta, :name => "title", :content => title) + "\n"
  str += tag(:meta, :name => "description", :content => page_description) + "\n"

  str += tag(:meta, :property => "og:title", :content => title) + "\n"
  str += tag(:meta, :property => "og:description", :content => page_description) + "\n"
  str += tag(:meta, :property => "og:url", :content => request.url) + "\n"
  str += tag(:meta, :property => "og:site_name", :content => Settings.app_name) + "\n"
  str += tag(:meta, :property => "og:type", :content => "article") + "\n"

  return str unless page_object.respond_to?(:post_type)

  str += case page_object.post_type.to_s
  when "video"
    unless page_object.videos.empty?
      v = page_object.videos.first
      tag(:link, :rel => "video_src", :href => v.embed_url, :title => title) + "\n" +
      tag(:meta, :rel => "video_height", :content => v.height) + "\n" +
      tag(:meta, :rel => "video_width", :content => v.width) + "\n" +
      tag(:meta, :rel => "video_type", :content => "application/x-shockwave-flash") + "\n" +
      tag(:link, :rel => "image_src", :href => v.thumb_url, :title => title) + "\n" +
      tag(:meta, :property => "og:video", :content => v.embed_url) + "\n" +
      tag(:meta, :property => "og:video:height", :content => v.height) + "\n" +
      tag(:meta, :property => "og:video:width", :content => v.width) + "\n" +
      tag(:meta, :property => "og:video:type", :content => "application/x-shockwave-flash") + "\n" +
      tag(:meta, :property => "og:image", :content => v.thumb_url)
    end
  else
    if page_object.thumbnail
      tag(:link, :rel => "image_src", :href => page_object.thumbnail, :title => title) + "\n" +
      tag(:meta, :name => "medium", :content => "image") + "\n" +
      tag(:meta, :property => "og:image", :content => page_object.thumbnail) + "\n"
    end
  end
  str.html_safe
end

#flash_messagesObject

Common



66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/atreides/application_helper.rb', line 66

def flash_messages
  return unless messages = flash.keys.select{|k| [:notice, :message, :warning, :error].include?(k)}
  (:div, :id => "flash") do
    messages.map do |type|
       :div, :id => "flash-#{type.to_s}", :class => "flash #{type.to_s}" do
         :span, message_for_item(flash[type], flash["#{type}_item".to_sym]).html_safe
      end
    end.join.html_safe
  end
end


89
90
91
# File 'app/helpers/atreides/application_helper.rb', line 89

def link_current_block
  @link_current_block ||= Proc.new { |name| (:span, name) }
end

#message_for_item(message, item = nil) ⇒ Object



77
78
79
80
81
82
83
# File 'app/helpers/atreides/application_helper.rb', line 77

def message_for_item(message, item = nil)
  if item.is_a?(Array)
    message % link_to(*item)
  else
    message
  end
end

#page_descriptionObject



52
53
54
55
# File 'app/helpers/atreides/application_helper.rb', line 52

def page_description
  return "" if page_object.nil?
  truncate_words(strip_tags(page_object.body), :length => 20, :omission => "...")
end

#page_objectObject



57
58
59
60
61
# File 'app/helpers/atreides/application_helper.rb', line 57

def page_object
  page_object ||= @post || @page
  page_object ||= @posts.first if @posts
  page_object
end

#page_titleObject



46
47
48
49
50
# File 'app/helpers/atreides/application_helper.rb', line 46

def page_title
  return "" if page_object.nil?

  page_object.title
end

#remote_function(options) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/helpers/atreides/application_helper.rb', line 129

def remote_function(options)
  opts = {
    :type     => options[:type] || options[:method] || 'post',
    :dataType => options[:dataType] || 'script',
    :url      => options[:url] || url_for(params)
  }
  opts.each_pair{|k,v| opts[k] = "'#{v}'" }
  opts[:data] = options[:data] if options.key?(:data)

  [:success, :complete, :beforeSend, :error].each do |k|
    opts[k] = "function(){" + options[k] + "}" if options.key?(k)
  end

  %($.ajax(#{options_for_javascript(opts)});)
end

#session_keyObject



85
86
87
# File 'app/helpers/atreides/application_helper.rb', line 85

def session_key
  @session_key ||= Rails.application.config.session_options[:key]
end

#sort_col(col) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'app/helpers/atreides/application_helper.rb', line 176

def sort_col(col)
  col_sym = col.class.to_s.downcase.to_sym rescue nil
  case col_sym
  when :datetime, :time
    "date-iso"
  else
    "text"
  end
end

#sortable_element(element_id, options = {}) ⇒ Object



125
126
127
# File 'app/helpers/atreides/application_helper.rb', line 125

def sortable_element(element_id, options = {})
  javascript_tag(sortable_element_js(element_id, options))
end

#sortable_element_js(element_id, options = {}) ⇒ Object

ScriptaculousHelper overrides for jQuery



112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/helpers/atreides/application_helper.rb', line 112

def sortable_element_js(element_id, options = {}) #:nodoc:
  # Make AJAX callback request if URL provided
  if options.key?(:url)
    options[:data] = %($(#{ActiveSupport::JSON.encode(element_id)}).sortable('serialize'))
    options[:update] = "function(){" + remote_function(options) + "}"
    options.delete(:url)
    options.delete(:data)
  end
  [:axis].each {|k| options[k] = "'#{options[k]}'" }

  %($(#{ActiveSupport::JSON.encode(element_id)}).sortable(#{options_for_javascript(options)});)
end

#truncate_words(txt, ops = {}) ⇒ Object



100
101
102
103
104
105
106
107
# File 'app/helpers/atreides/application_helper.rb', line 100

def truncate_words(txt, ops = {})
  ops.reverse_merge({
    :length => 100,
    :omission   => "..."
  })
  words = txt.to_s.split()
  words[0..(ops[:length]-1)].join(' ').to_s + (words.length > ops[:length] ? ops[:omission] : '').to_s
end