Skip to content
This repository was archived by the owner on Apr 30, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/models/asset_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ class TagError < StandardError; end
desc %{
Renders an image tag for the asset.

Using the optional @size@ attribute, different sizes can be display.
Using the optional @size@ attribute, different sizes can be displayed.
“thumbnail” and “icon” sizes are built in, but custom ones can be set
using by changing assets.addition_thumbnails in the Radiant::Config
settings.
by changing assets.addition_thumbnails in the Radiant::Config settings.

*Usage:*
<pre><code><r:assets:image [title="asset_title"] [size="icon|thumbnail"]></code></pre>
Expand All @@ -129,10 +128,14 @@ class TagError < StandardError; end
geometry = options['geometry'] ? options.delete('geometry') : nil
#This is very experimental and will generate new sizes on the fly
asset.generate_style(size, { :size => geometry }) if geometry

alt = " alt='#{asset.title}'" unless tag.attr['alt'] rescue nil

width = (options.delete('width') || asset.width(size)).to_i
height = (options.delete('height') || asset.height(size)).to_i
dimensions = %Q{ width="#{width}" height="#{height}"} if width > 0 && height > 0
alt = %Q{ alt="#{h asset.title}"} unless tag.attr['alt'] rescue nil
attributes = options.inject('') { |s, (k, v)| s << %{#{k.downcase}="#{v}" } }.strip
attributes << alt unless alt.nil?
attributes << dimensions unless dimensions.nil?
url = asset.thumbnail(size)
%{<img src="#{url}" #{attributes unless attributes.empty?} />} rescue nil
end
Expand Down
6 changes: 3 additions & 3 deletions app/views/admin/assets/_assets_container.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@

- asset_panes.search do
#search-assets.pane
- form_tag do
- form_remote_tag :url => admin_assets_path, :method => :get, :update => 'search-results' do
%p
%label.note{ :for => 'search' }
= t("paperclipped.search_assets")
%input{ :type => "hidden", :name => "search", :value => @page.id }
%input{ :type => "hidden", :name => "asset_page", :value => @page.id }
%input{ :type => "search", :id => "search", :name => "search" }
= submit_tag t("paperclipped.search")
#search-results
.clear
= observe_field 'search', :frequency => 1, :update => 'search-results', :url => admin_assets_path(:asset_page => @page), :method => 'get', :with => "'search=' + escape(value)"
= observe_field 'search', :frequency => 1, :update => 'search-results', :url => admin_assets_path(:asset_page => @page), :method => 'get', :with => "'search=' + escape(value)"
27 changes: 14 additions & 13 deletions lib/tasks/assets_extension_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ namespace :radiant do
# cp file, local_tasks_path, :verbose => false
# end
# end
end

desc "Syncs all available translations for this ext to the English ext master"
task :sync => :environment do
# The main translation root, basically where English is kept
language_root = PaperclippedExtension.get_translation_keys(language_root)
desc "Syncs all available translations for this ext to the English ext master"
task :sync => :environment do
# The main translation root, basically where English is kept
language_root = PaperclippedExtension.get_translation_keys(language_root)
words = TranslationSupport.get_translation_keys(language_root)

Dir["#{language_root}/*.yml"].each do |filename|
next if filename.match('_available_tags')
basename = File.basename(filename, '.yml')
puts "Syncing #{basename}"
(comments, other) = TranslationSupport.read_file(filename, basename)
words.each { |k,v| other[k] ||= words[k] } # Initializing hash variable as empty if it does not exist
other.delete_if { |k,v| !words[k] } # Remove if not defined in en.yml
TranslationSupport.write_file(filename, basename, comments, other)
end
Dir["#{language_root}/*.yml"].each do |filename|
next if filename.match('_available_tags')
basename = File.basename(filename, '.yml')
puts "Syncing #{basename}"
(comments, other) = TranslationSupport.read_file(filename, basename)
words.each { |k,v| other[k] ||= words[k] } # Initializing hash variable as empty if it does not exist
other.delete_if { |k,v| !words[k] } # Remove if not defined in en.yml
TranslationSupport.write_file(filename, basename, comments, other)
end
end

Expand Down
69 changes: 69 additions & 0 deletions spec/models/asset_tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe 'Asset Tags' do
dataset :pages

describe '<r:assets:image>' do
before(:each) do
@image = Asset.create({
:asset_file_name => 'foo.jpg',
:asset_content_type => 'image/jpeg',
:asset_file_size => '24680'
})
Asset.stub!(:find).and_return(@image)
end

it 'should render an img tag by ID with a default alt text' do
tag = %{<r:assets:image id="#{@image.id}" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"foo\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should render an img tag by title with a default alt text' do
tag = %{<r:assets:image title="My Title" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"foo\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should use the given title as the alt text' do
@image.stub!(:title).and_return('My Title')
tag = %{<r:assets:image id="#{@image.id}" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"My Title\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should escape the title in the alt text' do
@image.stub!(:title).and_return(%{Harry "Snapper" Organs & Stig O'Tracey})
tag = %{<r:assets:image id="#{@image.id}" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"Harry &quot;Snapper&quot; Organs &amp; Stig O'Tracey\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should use the given alt text' do
tag = %{<r:assets:image id="#{@image.id}" alt="Alternate title" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"Alternate title\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should include the image dimensions' do
@image.stub!(:dimensions).and_return([200,100])
tag = %{<r:assets:image id="#{@image.id}" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"foo\" +width=\"200\" height=\"100\" */>\z}

pages(:home).should render(tag).matching(expected)
end

it 'should override the image dimensions with the given dimensions' do
@image.stub!(:dimensions).and_return([200,100])
tag = %{<r:assets:image id="#{@image.id}" width="100" height="50" />}
expected = %r{\A<img +src=\"/assets/#{@image.id}/foo.jpg\" +alt=\"foo\" +width=\"100\" height=\"50\" */>\z}

pages(:home).should render(tag).matching(expected)
end
end
end
7 changes: 3 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/boot"
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../")}/config/environment"
end
require "#{RADIANT_ROOT}/spec/spec_helper"

require 'spec/rails'

# require File.expand_path(File.dirname(__FILE__) + "/blueprints")
# Dataset::Resolver.default << (File.dirname(__FILE__) + "/datasets")

Spec::Runner.configure do |config|
config.use_transactional_fixtures = true
Expand All @@ -24,4 +23,4 @@
#
# If you declare global fixtures, be aware that they will be declared
# for all of your examples, even those that don't use them.
end
end