Skip to content

Commit

Permalink
Updated images and resources plugin to group files by date uploaded i…
Browse files Browse the repository at this point in the history
…n list view if a refinery setting is set to true to allow them. Fixed the HTML Truncation on the news section by adding a third party library and integrating it into the Refinery core. Updated WYMeditor to fix an issue where highlighting a link would not recognise it as a link when using dialogues (only for mozilla browsers currently -- Safari already worked). Improved refinery-update-core task by allowing it to hook into the environment file to add any new gems used by Refinery. Added dependency to Hpricot gem for use with the HTML parser (hpricot version 0.8.1).
  • Loading branch information
parndt committed Nov 18, 2009
1 parent 4acf949 commit c43c611
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 65 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -9,4 +9,6 @@ public/images/system/*
nbproject
index/**/*
db/*.sqlite3
*.tmproj
*.tmproj
*.autobackupbyrefinery.*
refinerycms*.gem
28 changes: 28 additions & 0 deletions bin/refinery-update-core
Expand Up @@ -20,10 +20,38 @@ unless RAILS_ROOT.nil? or RAILS_ROOT.length == 0
# copy any rake tasks from plugins to the main lib directory so they can be run.
FileUtils::cp Dir[File.join(REFINERY_ROOT, %w(** tasks *.rake))], File.join(RAILS_ROOT, %w(lib tasks))

# read in the config files
app_config = File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "r").read
environment_updated = false

# backup app's environment.rb
unless (app_refinery_gems_section = app_config.scan(/(#===REFINERY REQUIRED GEMS===)([^#]*)(#===REFINERY END OF REQUIRED GEMS===)/).join("")).length == 0
# read in the config file in the gem
refinery_config = File.open(File.join(REFINERY_ROOT, %w(config environment.rb)), "r").read
FileUtils.cp File.join(RAILS_ROOT, %w(config environment.rb)), File.join(RAILS_ROOT, %w(config environment.autobackupbyrefinery.rb))

# write the new content into the file.
File.open(File.join(RAILS_ROOT, %w(config environment.rb)), "w").puts(app_config.gsub!(
app_refinery_gems_section,
refinery_config.scan(/(#===REFINERY REQUIRED GEMS===)([^#]*)(#===REFINERY END OF REQUIRED GEMS===)/).join("")
))

environment_updated = true
end

unless ARGV.include?("--from-refinery-installer")
puts "---------"
puts "Copied new Refinery core assets."
if environment_updated
puts "I've made a backup of your current config/environment.rb file as it has been updated with the latest Refinery RubyGem requirements."
puts "The backup is located at config/environment.autobackupbyrefinery.rb incase you need it."
end
puts ""
puts "=== ACTION REQUIRED ==="
puts "Please run rake db:migrate to ensure your database is at the correct version."
puts "Please also run rake gems:install to ensure you have the currently specified gems." if environment_updated

puts ""
end
else
unless ARGV.include?("--from-refinery-installer")
Expand Down
9 changes: 7 additions & 2 deletions config/environment.rb
Expand Up @@ -7,7 +7,7 @@
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
# Freeze to a specific version of refinerycms when running as a gem
# REFINERY_GEM_VERSION = 0.9.5.1 unless defined? REFINERY_GEM_VERSION
# REFINERY_GEM_VERSION = 0.9.5.10 unless defined? REFINERY_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Expand Down Expand Up @@ -59,12 +59,17 @@
# Make Active Record use UTC-base instead of local time
# config.active_record.default_timezone = :utc
#


# Please add your gems above the Refinery required gems here:

#===REFINERY REQUIRED GEMS===
config.gem "rake", :version => ">= 0.8.3", :lib => "rake"
config.gem "friendly_id", :version => ">= 2.2.2", :lib => "friendly_id"
config.gem "will_paginate", :version => ">= 2.3.11", :lib => "will_paginate", :source => "http://gemcutter.org"
config.gem "rails", :version => ">= 2.3.4", :lib => "rails"
config.gem "aasm", :version => ">= 2.1.3", :lib => "aasm", :source => "http://gemcutter.org"
config.gem "unicode", :version => ">= 0.1", :lib => "unicode"
config.gem "slim_scrooge", :source => "http://gemcutter.org"
config.gem "hpricot", :version => "0.8.1", :lib => "hpricot", :source => "http://gemcutter.org"
#===REFINERY END OF REQUIRED GEMS===
end
14 changes: 14 additions & 0 deletions public/javascripts/wymeditor/jquery.refinery.wymeditor.js
Expand Up @@ -1222,6 +1222,20 @@ WYMeditor.editor.prototype.dialog = function( dialogType ) {
}

selected = this.selected();
if (dialogType == WYMeditor.DIALOG_LINK && jQuery.browser.mozilla) {
selection = this._iframe.contentWindow.getSelection();
matches = selected.innerHTML.match(new RegExp(selection.anchorNode.textContent + "(.*)" + selection.focusNode.textContent));
if (matches != null && matches.length > 0 && (possible_anchor_tag = matches.last()) != null) {
if ((href = possible_anchor_tag.match(/href="([^"]*)"/).last()) != null) {
possible_anchors = this._iframe.document().getElementsByTagName('a');
for (i=0;i<possible_anchors.length;i++) {
if ((possible_match = possible_anchors[i]).innerHTML == selection) {
selected = possible_match;
}
}
}
}
}

// set up handlers.
imageGroup = null;
Expand Down
6 changes: 3 additions & 3 deletions refinerycms.gemspec

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions vendor/plugins/images/app/controllers/admin/images_controller.rb
Expand Up @@ -5,6 +5,28 @@ class Admin::ImagesController < Admin::BaseController
crudify :image, :order => "created_at DESC", :conditions => "parent_id is NULL", :sortable => false
before_filter :change_list_mode_if_specified

def index
if searching?
@images = Image.paginate_search params[:search],
:page => params[:page],
:order => "created_at DESC",
:conditions => "parent_id IS NULL"
else
@images = Image.paginate :page => params[:page],
:order => "created_at DESC",
:conditions => "parent_id IS NULL"
end

if RefinerySetting.find_or_set(:group_images_by_date_uploaded, false)
@grouped_images = []
@images.each do |image|
key = image.created_at.strftime("%Y-%m-%d")
image_group = @grouped_images.collect{|images| images.last if images.first == key }.flatten.compact << image
(@grouped_images.delete_if {|i| i.first == key}) << [key, image_group]
end
end
end

def new
@image = Image.new
@url_override = admin_images_url(:dialog => from_dialog?)
Expand Down
33 changes: 12 additions & 21 deletions vendor/plugins/images/app/views/admin/images/_list_view.html.erb
@@ -1,21 +1,12 @@
<ul>
<% @images.each do |image| -%>
<% if image.parent_id.nil? %>
<li id="sortable_<%= image.id %>" class='clearfix record <%= cycle("on", "on-hover") %>'>
<span class='title'>
<span class='actions'>
<%= link_to refinery_icon_tag('eye.png'), image.public_filename,
:target => "_blank", :title => "#{image_fu image, :preview}" %>
<%= link_to refinery_icon_tag('application_edit.png'), edit_admin_image_path(image),
:title => 'Edit this image' %>
<%= link_to refinery_icon_tag('delete.png'), admin_image_path(image),
:confirm => "Are you sure you want to delete #{image.title}?",
:class => "cancel", :method => :delete,
:title => "Remove this image forever" %>
</span>
<%= image.title %> <span class="preview">&nbsp;</span>
</span>
</li>
<% end %>
<% end -%>
</ul>
<% if RefinerySetting.find_or_set(:group_images_by_date_uploaded, false) %>
<% @grouped_images.each do |container| %>
<h3><%= (image_group = container.last).first.created_at.strftime("%A, %d %B %Y") %></h3>
<ul>
<%= render :partial => 'list_view_image', :collection => image_group %>
</ul>
<% end %>
<% else %>
<ul>
<%= render :partial => 'list_view_image', :collection => @images %>
</ul>
<% end %>
@@ -0,0 +1,17 @@
<% if list_view_image.parent_id.nil? %>
<li id="sortable_<%= list_view_image.id %>" class='clearfix record <%= cycle("on", "on-hover") %>'>
<span class='title'>
<span class='actions'>
<%= link_to refinery_icon_tag('eye.png'), list_view_image.public_filename,
:target => "_blank", :title => "#{image_fu list_view_image, :preview}" %>
<%= link_to refinery_icon_tag('application_edit.png'), edit_admin_image_path(list_view_image),
:title => 'Edit this image' %>
<%= link_to refinery_icon_tag('delete.png'), admin_image_path(list_view_image),
:confirm => "Are you sure you want to delete #{list_view_image.title}?",
:class => "cancel", :method => :delete,
:title => "Remove this image forever" %>
</span>
<%= list_view_image.title %> <span class="preview">&nbsp;</span>
</span>
</li>
<% end %>
36 changes: 1 addition & 35 deletions vendor/plugins/refinery/lib/refinery/application_helper.rb
@@ -1,5 +1,6 @@
# Methods added to this helper will be available to all templates in the application.
module Refinery::ApplicationHelper
include Refinery::HtmlTruncationHelper

def add_meta_tags
content_for :head, "<meta name=\"keywords\" content=\"#{@page.meta_keywords}\" />" unless @page.meta_keywords.blank?
Expand Down Expand Up @@ -80,41 +81,6 @@ def image_fu(image, thumbnail = nil , options={})
def refinery_icon_tag(filename, options = {})
image_tag "refinery/icons/#{filename}", {:width => 16, :height => 16}.merge!(options)
end

def truncate(text, *args)
truncated = super
unless truncated === text
options = args.extract_options!
if options[:preserve_html_tags]
truncated = truncated[0, truncated.length - options[:omission].length]
# scan for all HTML tags then the last tag
unless (tag_matches = truncated.scan(/<[^<>]{1,}>?/)).empty? or
(last_tag = tag_matches.last.gsub(/<|\/|\>/, "").split(" ").first).nil? or
last_tag.downcase == "p" or (tag_matches.last.split("<").last.first == "/")

# scan until we find the exact tag we're looking for.
full_tag = text.scan(Regexp.new("(#{tag_matches.last})([^<]*)(<\/#{last_tag}>)"))[0..2].join("") # 0..2 because it'll be found as 3 tags together.
truncated.gsub!(tag_matches.last, full_tag)

end

truncated = truncated[0, truncated.length-1] if truncated.last == "<" # If a tag almost started, get rid of it.

# add back on the omission
truncated = "#{truncated}#{options[:omission]}"

# close the paragraph unless it closes already.
start_index = truncated.length-"</p>".length-options[:omission].length
length = (end_index = options[:omission].length + "</p>".length)
unless truncated[start_index, length].downcase == "</p>"
truncated = "#{truncated}</p>"
end

end
end

truncated
end

# replace all system images with a thumbnail version of them (handy for all images inside a page part)
def content_fu(content, thumbnail)
Expand Down
114 changes: 114 additions & 0 deletions vendor/plugins/refinery/lib/refinery/html_truncation_helper.rb
@@ -0,0 +1,114 @@
# By Henrik Nyh <http://henrik.nyh.se> 2008-01-30.
# Free to modify and redistribute with credit.

# modified by Dave Nolan <http://textgoeshere.org.uk> 2008-02-06
# Ellipsis appended to text of last HTML node
# Ellipsis inserted after final word break

# modified by Mark Dickson <mark@sitesteaders.com> 2008-12-18
# Option to truncate to last full word
# Option to include a 'more' link
# Check for nil last child

# modified by Ken-ichi Ueda <http://kueda.net> 2009-09-02
# Rails 2.3 compatability (chars -> mb_chars), via Henrik
# Hpricot 0.8 compatability (avoid dup on Hpricot::Elem)

# modified by Philip Arndt <http://www.resolvedigital.co.nz> 2009-11-18
# renamed function to truncate and activated html truncation when :preserve_html_tags is supplied and true.
# modified namespace to Refinery namespace and renamed module.

require "rubygems"
require "hpricot"

module Refinery::HtmlTruncationHelper

# Like the Rails _truncate_ helper but doesn't break HTML tags, entities, and optionally. words.
def truncate(text, *args)
return if text.nil?
options = args.extract_options!
unless (options[:preserve_html_tags] == true)
return super
else
max_length = options[:length] || 30
omission = options[:omission] || "..."
# use :link => link_to('more', post_path), or something to that effect

doc = Hpricot(text.to_s)
omission_length = Hpricot(omission).inner_text.mb_chars.length
content_length = doc.inner_text.mb_chars.length
actual_length = max_length - omission_length

if content_length > max_length
truncated_doc = doc.truncate(actual_length)

if (options[:preserve_full_words] || false)
word_length = actual_length - (truncated_doc.inner_html.mb_chars.length - truncated_doc.inner_html.rindex(' '))
truncated_doc = doc.truncate(word_length)
end

last_child = truncated_doc.children.last
if last_child.inner_html.nil?
truncated_doc.inner_html + omission + options[:link] if options[:link]
else
last_child.inner_html = last_child.inner_html.gsub(/\W.[^\s]+$/, "") + omission
last_child.inner_html += options[:link] if options[:link]
truncated_doc
end
else
if options[:link]
last_child = doc.children.last
if last_child.inner_html.nil?
doc.inner_html + options[:link]
else
last_child.inner_html = last_child.inner_html.gsub(/\W.[^\s]+$/, "") + options[:link]
doc
end
else
text.to_s
end
end
end
end


end

module HpricotTruncator
module NodeWithChildren
def truncate(max_length)
return self if inner_text.mb_chars.length <= max_length
truncated_node = if self.is_a?(Hpricot::Doc)
self.dup
else
self.class.send(:new, self.name, self.attributes)
end
truncated_node.children = []
each_child do |node|
remaining_length = max_length - truncated_node.inner_text.mb_chars.length
break if remaining_length <= 0
truncated_node.children << node.truncate(remaining_length)
end
truncated_node
end
end

module TextNode
def truncate(max_length)
# We're using String#scan because Hpricot doesn't distinguish entities.
Hpricot::Text.new(content.scan(/&#?[^\W_]+;|./).first(max_length).join)
end
end

module IgnoredTag
def truncate(max_length)
self
end
end
end

Hpricot::Doc.send(:include, HpricotTruncator::NodeWithChildren)
Hpricot::Elem.send(:include, HpricotTruncator::NodeWithChildren)
Hpricot::Text.send(:include, HpricotTruncator::TextNode)
Hpricot::BogusETag.send(:include, HpricotTruncator::IgnoredTag)
Hpricot::Comment.send(:include, HpricotTruncator::IgnoredTag)
Expand Up @@ -29,6 +29,26 @@ def create
end
end

def index
if searching?
@resources = Resource.paginate_search params[:search],
:page => params[:page],
:order => "created_at DESC"
else
@resources = Resource.paginate :page => params[:page],
:order => "created_at DESC"
end

if RefinerySetting.find_or_set(:group_resources_by_date_uploaded, false)
@grouped_resources = []
@resources.each do |resource|
key = resource.created_at.strftime("%Y-%m-%d")
resource_group = @grouped_resources.collect{|resources| resources.last if resources.first == key }.flatten.compact << resource
(@grouped_resources.delete_if {|i| i.first == key}) << [key, resource_group]
end
end
end

def insert
self.new if @resource.nil?
@dialog = from_dialog?
Expand Down
15 changes: 12 additions & 3 deletions vendor/plugins/resources/app/views/admin/resources/index.html.erb
Expand Up @@ -19,9 +19,18 @@
<% else %>
<% if @resources.size > 0 -%>
<%= will_paginate @resources, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<ul>
<%= render :partial => "resource", :collection => @resources %>
</ul>
<% if RefinerySetting.find_or_set(:group_resources_by_date_uploaded, false) %>
<% @grouped_resources.each do |container| %>
<h3><%= (resource_group = container.last).first.created_at.strftime("%A, %d %B %Y") %></h3>
<ul>
<%= render :partial => "resource", :collection => resource_group %>
</ul>
<% end %>
<% else %>
<ul>
<%= render :partial => "resource", :collection => @resources %>
</ul>
<% end %>
<%= will_paginate @resources, :previous_label => '&laquo;', :next_label => '&raquo;' %>
<% else -%>
<p>
Expand Down

0 comments on commit c43c611

Please sign in to comment.