Module: ActionControllerExtensions::InstanceMethods
- Defined in:
- lib/extensions/action_controller.rb
Instance Method Summary collapse
- #erb_render(content, safe_level = 3, rethrow_exceptions = false) ⇒ Object
-
#expire_session_data ⇒ Object
:nodoc:.
-
#insert_object(name, type = :text, options = {}, html_options = {}) ⇒ Object
valid options: * :include_tags => ‘tags, to, include’ * :exclude_tags => ‘tags, to, exclude’.
- #render_page_list_segment(name, pages, options = {}, html_options = {}) ⇒ Object
Instance Method Details
#erb_render(content, safe_level = 3, rethrow_exceptions = false) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/extensions/action_controller.rb', line 176 def erb_render(content, safe_level = 3, rethrow_exceptions = false) # sanitize possibly dangerous content before rendering content.gsub!(/<(%.*?(exec|system)\s?\(.*?\s*%)>/, '<\1>') content.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '<\1>') content.gsub!(/<(%.*?\`.*?\s*%)>/, '<\1>') render_to_string(:inline => content.to_s).html_safe end |
#expire_session_data ⇒ Object
:nodoc:
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 |
# File 'lib/extensions/action_controller.rb', line 7 def expire_session_data # :nodoc: # make sure this is not the first run (session being initialized) if session[:last_active] idle_time = Time.now - session[:last_active] # expire session data as necessary # session_data = session.instance_variable_get("@data") # session_data.select { |k,v| k.to_s !~ /_expiration$/ && v }.each do |k,v| # idx = k.to_s + '_expiration' # if (exp = (session[idx] || session[idx.to_sym]).to_i) > 0 # if idle_time > exp # logger.debug "Expiring #{k} = #{v} (expiration #{exp} > idle time #{idle_time})" # session[k] = nil # else # logger.debug "Retaining #{k} = #{v} (expiration #{exp} < idle time #{idle_time})" # end # else # #logger.debug "Retaining #{k} = #{v} (does not expire)" # end # end end # bump/set last active time session[:last_active] = Time.now if is_logged_in_user? [:user_auth_status] = { :value => 'authenticated', :expires => 5.minutes.from_now } end end |
#insert_object(name, type = :text, options = {}, html_options = {}) ⇒ Object
valid options:
-
:include_tags => ‘tags, to, include’
-
:exclude_tags => ‘tags, to, exclude’
40 41 42 43 44 45 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 75 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 |
# File 'lib/extensions/action_controller.rb', line 40 def insert_object(name, type = :text, = {}, = {}) extend ActionView::Helpers::TagHelper extend ActionView::Helpers::TextHelper @page_objects ||= {} key = "obj-#{type.to_s}-#{name.gsub(/[^\w]/, '_')}" case type.to_sym when :string content = substitute_placeholders(@page_objects[key] || '', @pg) content = erb_render(content) content = auto_link(content, :urls, :target => '_blank') unless [:disable_auto_link] content = auto_link_email_addresses(content) unless [:disable_auto_link] content_tag :span, content, when :text content = substitute_placeholders(@page_objects[key] || '', @pg) content = erb_render(content) content = auto_link(content, :urls, :target => '_blank') unless [:disable_auto_link] content = auto_link_email_addresses(content) unless [:disable_auto_link] content_tag :div, content, when :page_list @rss_feeds ||= [] @rss_feeds << name case @page_objects["#{key}-style-display-as"] when 'calendar' pages = page_list_items(@pg, key, ).compact.uniq. sort { |a,b| (a.position || 0) <=> (b.position || 0) }. sort { |a,b| (b.article_date || b.published_date || Time.now) <=> (a.article_date || a.published_date || Time.now) } render :partial => 'page_list_calendar', :locals => { :key => key, :pages => pages } else # display as 'list' today = Time.utc(Time.now.year, Time.now.month, Time.now.day) case @page_objects["#{key}-date-range"] when 'all' when 'past' [:end_date] ||= today when 'future' [:start_date] ||= today when 'custom' [:start_date] ||= @page_objects["#{key}-date-range-custom-start"] [:end_date] ||= @page_objects["#{key}-date-range-custom-end"] end pages = page_list_items(@pg, key, ).compact.uniq [:wrapper_div] = true # make options specified in snippets and templates accessible to # page list segments and rss feeds @page_objects["#{key}-template"] = [:template] if @page_objects["#{key}-template"].blank? render_page_list_segment(name, pages, , ) end when :snippet @snippet = CmsSnippet.find_by_name(name) if @snippet erb_render(substitute_placeholders(@snippet.content, @pg)) else 'Could not find snippet "' + name + '" in the database.' end when :photo_gallery gallery_dir = File.join('assets', 'content', @pg.path, File.basename(name)) Dir.chdir(File.join(Rails.root, 'public')) all_images = Dir.glob("#{gallery_dir}/*.{jpg,jpeg,png,gif}") Dir.chdir(Rails.root) all_images.sort! { |a,b| File.basename(a).to_i <=> File.basename(b).to_i } images = all_images.reject { |img| img =~ /-thumb/ } thumbs = all_images.reject { |img| img !~ /-thumb/ } render_to_string(:partial => 'photo_gallery', :locals => { :name => name, :images => images, :thumbs => thumbs }).html_safe end end |
#render_page_list_segment(name, pages, options = {}, html_options = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/extensions/action_controller.rb', line 113 def render_page_list_segment(name, pages, = {}, = {}) extend ActionView::Helpers::TagHelper extend ActionView::Helpers::TextHelper extend ActionView::Helpers::JavaScriptHelper extend ActionView::Helpers::PrototypeHelper key = "obj-page_list-#{name.gsub(/[^\w]/, '_')}" offset = first_non_empty(params[:offset], 0).to_i limit = first_non_empty(@page_objects["#{key}-max-item-count"], [:item_count], pages.size).to_i limit = 1 if limit < 1 page_subset = pages[offset, limit] || [] content = '' content << substitute_placeholders(first_non_empty(@page_objects["#{key}-header"], [:header]), @pg, :count => page_subset.size, :total => pages.size, :rss_feed_url => (@pg && @pg.id ? url_for(:action => 'rss_feed', :page_id => @pg.id, :page_list_name => name) : nil)) if page_subset.empty? content << substitute_placeholders(first_non_empty(@page_objects["#{key}-empty_message"], [:empty_message], 'No pages found.'), @pg) else page_subset.each_with_index do |page, index| content << substitute_placeholders(first_non_empty(@page_objects["#{key}-template"], [:template], ''), page, :index => index+1, :count => page_subset.size, :total => pages.size) end end content << substitute_placeholders(first_non_empty(@page_objects["#{key}-footer"], [:footer]), @pg, :count => page_subset.size, :total => pages.size, :rss_feed_url => (@pg && @pg.id ? url_for(:action => 'rss_feed', :page_id => @pg.id, :page_list_name => name) : nil)) num_segments = (pages.size.to_f / limit).ceil if @page_objects["#{key}-use-pagination"].to_i == 1 && num_segments > 1 content << '<table style="margin-top: 4px;" align="right" cellpadding="0" cellspacing="0" border="0"><tr valign="bottom">' content << '<td>Page: </td>' num_segments.times do |seg| start = seg * limit content << "<td><a href=\"#\"" if offset >= start && offset < (start + limit) content << " class=\"page_list_segment page_list_segment_selected\"" else content << " class=\"page_list_segment\"" content << " onmouseover=\"this.className = 'page_list_segment page_list_segment_selected'\"" content << " onmouseout=\"this.className = 'page_list_segment'\"" content << " onclick=\"this.style.cursor = 'wait';" content << remote_function(:update => key, :url => { :content_path => @pg.path.split('/').concat([ 'segment', start.to_s, name ]) }) content << "; return false;\"" end content << ">#{seg+1}</a></td>" end content << '</tr></table>' end if [:wrapper_div] content_tag :div, erb_render(content), .update(:id => key) else erb_render(content) end end |