Skip to content

Commit

Permalink
merging with hairballopolis/radiant-gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Cowell committed Jun 20, 2009
1 parent ce5dedc commit 4a15698
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 107 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Expand Up @@ -124,10 +124,12 @@ If you want to generate that thumbnail just after the upload of the image, add t
admin_thumb: 300x300 # do not remove
admin_preview: 500x500 # do not remove
small: 200x100 # this is the signature you use in the page.
square: c90x90 # specific sized images with a center crop

== Contributors

* Version: 0.3.0 - Adam Salter - http://10outta10.com
* Version: 0.7.* - Tom Cowell - http://eightsquarestudio.com

== Credits

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/gallery_importings_controller.rb
Expand Up @@ -2,6 +2,8 @@

class GalleryImportingsController < ApplicationController

protect_from_forgery :except => [ :import ]

helper 'galleries'
layout 'gallery_popup'

Expand Down
21 changes: 20 additions & 1 deletion app/models/gallery.rb
Expand Up @@ -18,7 +18,9 @@ class Gallery < ActiveRecord::Base

belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by'
belongs_to :update_by, :class_name => 'User', :foreign_key => 'update_by'

has_and_belongs_to_many :gallery_keywords, :join_table => "galleries_keywords", :foreign_key => "gallery_id", :uniq => true,
:class_name => "GalleryKeyword", :association_foreign_key => "keyword_id"

attr_protected :slug, :path

validates_presence_of :name
Expand Down Expand Up @@ -49,6 +51,23 @@ def absolute_thumbs_path
File.join(self.absolute_path, 'thumbs')
end

def keywords
str =''
self.gallery_keywords.each do |key|
str += key.keyword
str += ','
end
str.slice(0..-2)
end

def keywords=(keywords)
self.gallery_keywords = []
keys = keywords.split(',')
keys.each do |word|
self.gallery_keywords << GalleryKeyword.find_or_create_by_keyword(word.strip)
end
end

def url(root_id = nil)
File.join((self.ancestors_from(root_id).reverse << self).collect{|a| a.slug})
end
Expand Down
31 changes: 28 additions & 3 deletions app/models/gallery_item.rb
Expand Up @@ -10,14 +10,14 @@ def [](extension)
@@extensions[extension.downcase] || 'Unknown'
end
end
end
end

attr_accessible :name, :description, :uploaded_data

has_attachment :storage => :file_system,
:path_prefix => Radiant::Config["gallery.path_prefix"],
:processor => Radiant::Config["gallery.processor"],
:max_size => 3.megabytes
:max_size => Radiant::Config["gallery.max_size"].to_i.kilobytes

belongs_to :gallery

Expand All @@ -27,6 +27,9 @@ def [](extension)
belongs_to :parent, :class_name => 'GalleryItem', :foreign_key => 'parent_id'

has_many :infos, :class_name => "GalleryItemInfo", :dependent => :delete_all

has_and_belongs_to_many :gallery_keywords, :join_table => "gallery_items_keywords", :foreign_key => "gallery_item_id", :uniq => true,
:class_name => "GalleryKeyword", :association_foreign_key => "keyword_id"

before_create :set_filename_as_name
before_create :set_position
Expand All @@ -37,7 +40,7 @@ def [](extension)
after_attachment_saved do |item|
item.generate_default_thumbnails if item.parent.nil?
end

before_thumbnail_saved do |thumbnail|
thumbnail.gallery_id = thumbnail.parent.gallery_id
end
Expand Down Expand Up @@ -68,6 +71,28 @@ def thumb(options = {})
end
end

def keywords
str = ''
if self.gallery_keywords.length > 0
self.gallery_keywords.uniq.each do |key|
str += key.keyword
str += ','
end
str = str.slice(0..-2)
else
str += self.gallery.keywords
end
return str
end

def keywords=(keywords)
self.gallery_keywords = []
keys = keywords.split(',')
keys.each do |word|
self.gallery_keywords << GalleryKeyword.find_or_create_by_keyword(word.strip)
end
end

def jpeg?
not (self.content_type =~ /jpeg/).nil?
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/gallery_keyword.rb
@@ -0,0 +1,6 @@
class GalleryKeyword < ActiveRecord::Base
has_and_belongs_to_many :gallery_items, :join_table => "gallery_items_keywords", :foreign_key => "keyword_id", :uniq => true,
:class_name => "GalleryItem", :association_foreign_key => "gallery_item_id"
has_and_belongs_to_many :galleries, :join_table => "galleries_keywords", :foreign_key => "keyword_id", :uniq => true,
:class_name => "Gallery", :association_foreign_key => "gallery_id"
end
6 changes: 5 additions & 1 deletion app/views/galleries/_form.html.erb
Expand Up @@ -15,10 +15,14 @@
</p>
<div>
<%= f.text_area "description", :class => 'textbox', :maxlength => 255 %>
</div>
<div>
<label for="gallery_keywords"> Keywords for gallery. </label>
<%= f.text_field "keywords", :class => 'textbox', :maxlength => 255 %>
</div>
<p id="advanced_options" style="display: none;">
<%= f.check_box :hidden, {:id => 'hide_gallery'} %>
<label for="hide_gallery"> Hide this gallery in galleries list. </label>
<label for="hide_gallery"> Hide this gallery in galleries list. </label>
</p>
<p>
<small id="close_advanced_options" style="display: none;"><a href="#" onclick="toggle_advanced(); return false;">Close advanced options</a></small>
Expand Down
5 changes: 4 additions & 1 deletion app/views/galleries/_gallery.html.erb
Expand Up @@ -19,10 +19,13 @@ spinner = image('spinner.gif', :class => 'busy', :id => "gallery-busy-#{gallery.
<a title="<%= gallery.name %>" href="<%= admin_gallery_path(gallery) %>">
<%= icon %>
<span class="name"><%= gallery.name %></span>
</a>
</a>
<small class="info">(<%= gallery.items.size %> files)</small>
<%= spinner %>
</span>
</td>
<td class='keywords'>
<span style='color:#666; font-size:0.75em;padding-left:0.5em;'><%= gallery.keywords if gallery.items.size > 0 && gallery.gallery_keywords.length > 0 %></span>
</td>
<td class="status"><%= gallery.id %></td>
<td class="status published-status"><%= gallery.hidden ? 'hidden' : 'visible' %></td>
Expand Down
3 changes: 2 additions & 1 deletion app/views/galleries/index.html.erb
Expand Up @@ -6,7 +6,8 @@
<table id="gallery_tree" class="index" cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th class="gallery">Name</th>
<th class="gallery">Name</th>
<th>Keywords <!-- link_to "(Manage Keywords)", --></th>
<th>ID</th>
<th class="status">Status</th>
<th class="modify" colspan="3">Actions</th>
Expand Down
4 changes: 4 additions & 0 deletions app/views/galleries/show.html.erb
Expand Up @@ -49,6 +49,10 @@
<div>
<label for="edit-item-description">Description</label>
<textarea id="edit-item-description" cols="40" rows="12"></textarea>
</div>
<div>
<label for="edit-item-keywords">Keywords</label>
<input type="text" id="edit-item-keywords" size="30">
</div>
<p>
<img id="edit-spinner" src="/images/admin/spinner.gif" style="display: none;"/>
Expand Down
1 change: 1 addition & 0 deletions app/views/gallery_items/_item.html.erb
Expand Up @@ -3,4 +3,5 @@
<%= item_preview(item) %>
<%= item_buttons(item) %>
<div class="description" id="item_<%= item.id %>_description" style="display:none;"><%= "#{item.description}" %></div>
<div class="keywords" id="item_<%= item.id %>_keywords" style="display:none;"><%= "#{item.keywords}" %></div>
</div>
4 changes: 3 additions & 1 deletion app/views/gallery_items/edit.html.erb
Expand Up @@ -9,7 +9,9 @@
<label for="update-item-popup-name-field">Name:</label> <br />
<%= f.text_field :name, :maxlength => 255 %> <br />
<label for="update-item-popup-description-field">Description:</label> <br />
<%= f.text_area :description %><br />
<%= f.text_area :description %><br />
<label for='update-item-popup-keywords-field'>Keywords:</label><br/>
<%= f.text_field :keywords, :maxlength => 255 %><br/>
<%= submit_tag "Save", :id => 'popup_save_button' %>
</div>
<% end %>
Expand Down
3 changes: 2 additions & 1 deletion app/views/gallery_items/update.js.rjs
@@ -1,3 +1,4 @@
page["item_#{@item.id}"].down('.label a').update(item_label_text(@item))
page["item_#{@item.id}"].down('.description').update(@item.description)
page["item_#{@item.id}"].down('.description').update(@item.description)
page["item_#{@item.id}"].down('.keywords').update(@item.keywords)
page.call 'Gallery.EditForm.close'
2 changes: 1 addition & 1 deletion config/gallery.yml.default
@@ -1,6 +1,6 @@
path_prefix: public/galleries
processor: rmagick
# max_size: 3.megabytes
max_size: 3000 #file max size in kilobytes
gallery_based: no
default_thumbnails:
admin_thumb: 300x300 # do not remove
Expand Down
28 changes: 28 additions & 0 deletions db/migrate/20090131152801_add_keywords.rb
@@ -0,0 +1,28 @@
class AddKeywords < ActiveRecord::Migration
def self.up
create_table :gallery_keywords, :unique => true, :force => true do |t|
t.column :keyword, :string
t.column :created_at, :datetime
t.column :updated_at, :datetime
end

create_table :galleries_keywords, :id => false, :unique => true, :force => true do |t|
t.column :gallery_id, :integer
t.column :keyword_id, :integer
end

create_table :gallery_items_keywords, :id => false, :unique => true, :force => true do |t|
t.column :gallery_item_id, :integer
t.column :keyword_id, :integer
end

end

def self.down
drop_table :table_name
drop_table :gallery_items_keywords
drop_table :galleries_keywords
drop_table :gallery_keywords
end
end

8 changes: 5 additions & 3 deletions gallery_extension.rb
@@ -1,5 +1,5 @@
require 'tempfile'
require_dependency 'application'
require_dependency 'application_controller'
require_dependency 'open-uri'
require_dependency 'exifr/jpeg'
require_dependency 'exifr/tiff'
Expand Down Expand Up @@ -36,8 +36,10 @@ class GalleryExtension < Radiant::Extension

def activate
init
tab_options = {:visibility => [:all]}
Radiant::Config["gallery.gallery_based"] == 'true' ? tab_options[:before] = "Pages" : tab_options[:after] = "Layouts"
tab_options = {:visibility => [:all]}
if Radiant::Config.table_exists?
Radiant::Config["gallery.gallery_based"] == 'true' ? tab_options[:before] = "Pages" : tab_options[:after] = "Layouts"
end
admin.tabs.add("Galleries", "/admin/galleries", tab_options)
admin.page.edit.add :layout_row, 'base_gallery' if admin.respond_to?(:page)
end
Expand Down
58 changes: 45 additions & 13 deletions lib/gallery_item_tags.rb
Expand Up @@ -11,12 +11,10 @@ class GalleryTagError < StandardError; end

desc %{
Usage:
<pre><code><r:gallery:items:each [order='order' by='by' limit='limit' offset='offset'
scope='all|gallery']>
...
</r:gallery:items:each></code></pre>
Iterates over all items in current gallery.
Valid scopes are 'all' (find all Gallery Items) and 'gallery' (find Items that belong to the current Gallery) }
<pre><code><r:gallery:items:each [order='order' by='by' limit='limit' offset='offset' scope='all|gallery'
keywords='key1,key2,key3' current_keywords='is|is_not']></r:gallery:items:each></code></pre>
Valid scopes are 'all' (find all Gallery Items) and 'gallery' (find Items that belong to the current Gallery)
Iterates through gallery items keywords=(manual entered keywords) and/or current_keywords=(is|is_not) }
tag "gallery:items:each" do |tag|
content = ""
gallery = find_gallery(tag)
Expand All @@ -38,14 +36,26 @@ class GalleryTagError < StandardError; end
options[:offset] = tag.attr['offset'] ? tag.attr['offset'].to_i : 0
options[:conditions] = {:parent_id => nil}

if !tag.attr['keywords'].nil? || !tag.attr['current_keywords'].nil?
keywords = !tag.attr['keywords'].nil? ? tag.attr['keywords'].split(',') : []
if (tag.attr['current_keywords'] == 'is' || tag.attr['current_keywords'] == 'is_not') && !tag.globals.page.request.parameters['keywords'].nil?
@current_keywords = tag.globals.page.request.parameters['keywords'].split(',') if !tag.globals.page.request.parameters['keywords'].nil?
if !@current_keywords.nil? && @current_keywords.length > 0
keywords.concat(@current_keywords)
end
end
options[:joins] = :gallery_keywords
options[:conditions].merge!({"gallery_keywords.keyword" => keywords}) if keywords.length > 0
end

@page_number = tag.globals.page.request.params["page"] && tag.globals.page.request.params["page"].first.to_i > 1 ? tag.globals.page.request.params["page"].first.to_i : 1
if !tag.attr['limit'].nil? && tag.attr['offset'].nil?
options[:offset] = tag.attr['limit'].to_i * (@page_number - 1)
@gallery_items_per_page = tag.attr['limit'].to_i
end

scope = tag.attr['scope'] ? tag.attr['scope'] : 'gallery'
raise GalleryTagError.new('Invalid value for attribute scope. Valid values are: gallery, all') unless %[gallery all].include?(scope)
raise GalleryTagError.new('Invalid value for attribute scope. Valid values are: gallery, all') unless %[gallery all].include?(scope)
items = case scope
when 'gallery'
gallery ? gallery.items.find(:all, options) : []
Expand Down Expand Up @@ -86,12 +96,34 @@ class GalleryTagError < StandardError; end

desc %{
Usage:
<pre><code><r:gallery:item:name /></code></pre>
Provides name for current gallery item }
<pre><code><r:gallery:item:name [safe='true']/></code></pre>
Provides name for current gallery item, safe is to make safe for web }
tag "gallery:item:name" do |tag|
item = find_item(tag)
item.name
end
item = find_item(tag)
if tag.attr['safe'] == 'true'
@safe = item.name.gsub(/[\s]+/, '_').downcase
else
@normal = item.name
end
name = tag.attr['safe'] ? @safe : @normal
end

desc %{
Usage:
<pre><code><r:gallery:item:keywords [separator=',' safe='true']/></code></pre>
Provides keywords for current gallery item, use
separator="separator_string" to specify the character between keywords}
tag "gallery:item:keywords" do |tag|
item = find_item(tag)
joiner = tag.attr['separator'] ? tag.attr['separator'] : ' '
if tag.attr['safe'] == 'true'
@safe = item.keywords.gsub(/[\s]+/, '_').downcase
else
@normal = item.keywords
end
keys = tag.attr['safe'] ? @safe : @normal
keys.gsub(/\,/, joiner);
end

desc %{
Usage:
Expand Down Expand Up @@ -131,7 +163,7 @@ class GalleryTagError < StandardError; end

desc %{
Usage:
<pre><code><r:gallery:item:page_url /></code></pre>
<pre><code><r:gallery:item:page_url/></code></pre>
Provides page url for current gallery item }
tag "gallery:item:page_url" do |tag|
item = find_item(tag)
Expand Down

0 comments on commit 4a15698

Please sign in to comment.