Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Commit

Permalink
Refactored existing extension to run inside a Rails 3 engine for use …
Browse files Browse the repository at this point in the history
…in Spree 0.30 and up
  • Loading branch information
joshmcarthur committed Jan 31, 2011
1 parent d15758f commit 49024db
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 266 deletions.
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the Rails Dog LLC nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Facebox

This is an extension for Spree, allowing to show zoomed product images to the customer:

It's based on Facebox:
http://github.com/defunkt/facebox

A lot of code was taken from this project:
http://github.com/eliotsykes/spree-zoom-photos

# Installation
Installation is quite simple:
* Add the gem source to your Gemfile
`gem 'spree_facebox', :git => 'https://github.com/jmcarthur/spree-facebox.git', :branch => 'rails3'`
* Run `bundle install` and then `rake spree_facebox:install:assets`

# Consequences
New extra large image size of 1000x1000. So when new image uploaded on server it will have a 1000x1000 size in addition to default sizes.
There is a way to transform existing images to the larger size, however do make sure that your original images are of a sufficient quality for this to be worthwhile - see the next section for different methods.

#Resizing existing images
**Resize *all* images**: `rake paperclip:refresh:thumbnails`
**Resize only 'Image' images**: `rake paperclip:refresh:thumnails CLASS=Image` (This is the one you want to resize just your Product/Variant images.
**Resize a selction of Product images**:
* Jump into a console and grab a selection of Products whose images you want to resize (using Array.select, or AR finders, etc).
* `[collection].each { |product| product.images.each { |image| image.reprocess! }}`


All wishes, suggestions and improvements are welcome!

146 changes: 20 additions & 126 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,137 +1,31 @@
# I think this is the one that should be moved to the extension Rakefile template

# In rails 1.2, plugins aren't available in the path until they're loaded.
# Check to see if the rspec plugin is installed first and require
# it if it is. If not, use the gem version.

# Determine where the RSpec plugin is by loading the boot
unless defined? SPREE_ROOT
ENV["RAILS_ENV"] = "test"
case
when ENV["SPREE_ENV_FILE"]
require File.dirname(ENV["SPREE_ENV_FILE"]) + "/boot"
when File.dirname(__FILE__) =~ %r{vendor/SPREE/vendor/extensions}
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
else
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
end
end
require File.expand_path('../../config/application', __FILE__)

require 'rubygems'
require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'
require 'rake/packagetask'
require 'rake/gempackagetask'

rspec_base = File.expand_path(SPREE_ROOT + '/vendor/plugins/rspec/lib')
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
# require 'spec/translator'

# Cleanup the SPREE_ROOT constant so specs will load the environment
Object.send(:remove_const, :SPREE_ROOT)
spec = eval(File.read('spree_facebox.gemspec'))

extension_root = File.expand_path(File.dirname(__FILE__))

task :default => :spec
task :stats => "spec:statsetup"

desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList["#{extension_root}/spec/**/*_spec.rb"]
Rake::GemPackageTask.new(spec) do |p|
p.gem_spec = spec
end

namespace :spec do
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--rails']
end

desc "Print Specdoc for all specs"
Spec::Rake::SpecTask.new(:doc) do |t|
t.spec_opts = ["--format", "specdoc", "--dry-run"]
t.spec_files = FileList['spec/**/*_spec.rb']
end

[:models, :controllers, :views, :helpers].each do |sub|
desc "Run the specs under spec/#{sub}"
Spec::Rake::SpecTask.new(sub) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end

# Hopefully no one has written their extensions in pre-0.9 style
# desc "Translate specs from pre-0.9 to 0.9 style"
# task :translate do
# translator = ::Spec::Translator.new
# dir = RAILS_ROOT + '/spec'
# translator.translate(dir, dir)
# end

# Setup specs for stats
task :statsetup do
require 'code_statistics'
::STATS_DIRECTORIES << %w(Model\ specs spec/models)
::STATS_DIRECTORIES << %w(View\ specs spec/views)
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
::CodeStatistics::TEST_TYPES << "Model specs"
::CodeStatistics::TEST_TYPES << "View specs"
::CodeStatistics::TEST_TYPES << "Controller specs"
::CodeStatistics::TEST_TYPES << "Helper specs"
::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
end

namespace :db do
namespace :fixtures do
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
task :load => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
end
end
end
end
end

desc 'Generate documentation for the facebox extension.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'FaceboxExtension'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
desc "Release to gemcutter"
task :release => :package do
require 'rake/gemcutter'
Rake::Gemcutter::Tasks.new(spec).define
Rake::Task['gem:push'].invoke
end

# For extensions that are in transition
desc 'Test the facebox extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

namespace :test do
desc 'Functional test the facebox extension.'
Rake::TestTask.new(:functionals) do |t|
t.libs << 'lib'
t.pattern = 'test/functional/*_test.rb'
t.verbose = true
end

desc 'Unit test the facebox extension.'
Rake::TestTask.new(:units) do |t|
t.libs << 'lib'
t.pattern = 'test/unit/*_test.rb'
t.verbose = true
end
end
desc "Default Task"
task :default => [ :spec ]

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
# require 'cucumber/rake/task'
# Cucumber::Rake::Task.new do |t|
# t.cucumber_opts = %w{--format pretty}
# end
8 changes: 8 additions & 0 deletions app/views/products/_facebox_image.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="product-image">
<% if image_controls.enable_zoom? %>
<%= link_to image_tag(image.attachment.url(size)), image.attachment.url(:xl), :rel => 'facebox' %>
<% else %>
<%= image_tag image.attachment.url(:product) %>
<% end %>
</div>

15 changes: 15 additions & 0 deletions app/views/products/_product_images.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div id="main-image">
<%= render :partial => 'facebox_image', :locals => {:image => @product.images.first, :size => :product} -%>
</div>
<div id="product-thumbnails" class="thumbnails">
<%= render :partial => 'facebox_image', :collection => @product.images[1..-1], :locals => {:size => :product} -%>
</div>
<% if @product.has_variants? -%>
<div id="variant-thumbnails" class="thumbnails">
<% @variants.each do |variant| %>
<h4><%= variant.name %></h4>
<%= render :partial => 'facebox_image', :collection => variant.images, :locals => {:size => :small} -%>
<% end -%>
</div>
<% end -%>

3 changes: 3 additions & 0 deletions app/views/shared/_facebox_include.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= stylesheet_link_tag('facebox') %>
<%= javascript_include_tag('facebox') %>

2 changes: 2 additions & 0 deletions app/views/shared/_script_for_facebox.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<%= javascript_tag "jQuery(document).ready(function() { jQuery('a[rel*=facebox]').facebox(); });" %>

8 changes: 3 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Put your extension routes here.

# map.namespace :admin do |admin|
# admin.resources :whatever
# end
Rails.application.routes.draw do
# Add your extension routes here
end
11 changes: 6 additions & 5 deletions lib/facebox/image_controls.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module Facebox
class ImageControls

def initialize(product)
@product = product
end

def enable_any?
enable_enlarge? || enable_zoom?
end

def enable_enlarge?
@enable_enlarge ||= !@product.images.blank? && !@product.has_image_without_style?(:large)
end

def enable_zoom?
@enable_zoom ||= !@product.images.blank? && !@product.has_image_without_style?(:xl)
end

end
end

51 changes: 51 additions & 0 deletions lib/spree_facebox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spree_core'
require 'spree_facebox_hooks'

module SpreeFacebox
class Engine < Rails::Engine

config.autoload_paths += %W(#{config.root}/lib)

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
Rails.env.production? ? require(c) : load(c)
end

#Add an extra large (xl) size to use for zooming.
Image.attachment_definitions[:attachment][:styles] =
{ :mini => '48x48>', :small => '100x100>', :product => '240x240>',
:large => '600x600>', :xl => '900x900>' }

ProductsHelper.class_eval do
def image_controls
@image_controls ||= Facebox::ImageControls.new(@product)
end
end

Product.class_eval do
def has_image_without_style?(style)
return true if contains_image_without_style?(images, style)
if !variants.blank?
variants.each do |variant|
return true if contains_image_without_style?(variant.images, style)
end
end
return false
end

private

def contains_image_without_style?(images, style)
return false if images.blank?
images.each do |image|
return true unless image.attachment.path(style)
end
return false
end
end
end

config.to_prepare &method(:activate).to_proc
end
end

7 changes: 7 additions & 0 deletions lib/spree_facebox_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SpreeFaceboxHooks < Spree::ThemeSupport::HookListener
# custom hooks go here
replace :product_images, 'products/product_images'
insert_after :product_images, 'shared/script_for_facebox'
insert_after :inside_head, 'shared/facebox_include'
end

26 changes: 26 additions & 0 deletions lib/tasks/install.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace :spree_facebox do
desc "Copies all migrations and assets (NOTE: This will be obsolete with Rails 3.1)"
task :install do
Rake::Task['spree_facebox:install:migrations'].invoke
Rake::Task['spree_facebox:install:assets'].invoke
end

namespace :install do
desc "Copies all migrations (NOTE: This will be obsolete with Rails 3.1)"
task :migrations do
source = File.join(File.dirname(__FILE__), '..', '..', 'db')
destination = File.join(Rails.root, 'db')
puts "INFO: Mirroring assets from #{source} to #{destination}"
Spree::FileUtilz.mirror_files(source, destination)
end

desc "Copies all assets (NOTE: This will be obsolete with Rails 3.1)"
task :assets do
source = File.join(File.dirname(__FILE__), '..', '..', 'public')
destination = File.join(Rails.root, 'public')
puts "INFO: Mirroring assets from #{source} to #{destination}"
Spree::FileUtilz.mirror_files(source, destination)
end
end

end
1 change: 1 addition & 0 deletions lib/tasks/spree_facebox.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# add custom rake tasks here
Binary file added public/images/facebox/closelabel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/facebox/loading.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 49024db

Please sign in to comment.