Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

view_helper specs added and validation module to help debug configura…

…tion og photos model
  • Loading branch information...
commit 45daac87f48da3d5cc2172439fe67325bb474900 1 parent 8a5be19
@kristianmandrup authored
View
42 README.md
@@ -4,7 +4,7 @@ Popular Javascript Photo galleries/carousels ready to use with Rails 3+.
## Usage
-`gem 'rails-gallery'
+`gem 'rails-gallery'`
## Galleries included
@@ -21,12 +21,11 @@ In `application.css` manifest file:
```css
/*
- * require responsive-gallery
- * require gallery/responsive/elastislide
- * require gallery/responsive/style
- * require gallery/slideshow
- * require gallery/galleria
- * require gallery/touch_touch
+ *= require gallery/responsive/elastislide
+ *= require gallery/responsive
+ *= require gallery/slideshow
+ *= require gallery/galleria/classic
+ *= require gallery/touch_touch
*/
```
@@ -36,7 +35,7 @@ Using Compass, f.ex in `application.css.scss`
@import 'gallery/responsive/elastislide';
@import 'gallery/responsive';
@import 'gallery/slideshow';
-@import 'gallery/galleria';
+@import 'gallery/galleria/classic';
@import 'gallery/touch_touch';
```
@@ -46,6 +45,7 @@ In `application.js` manifest file:
//= require gallery/responsive
//= require gallery/slideshow
//= require gallery/galleria
+//= require gallery/galleria/classic
//= require gallery/touch_touch
//= require jquery/jquery.easing-1.3
@@ -67,7 +67,7 @@ $(function(){
See [TouchTouch](http://tutorialzine.com/2012/04/mobile-touch-gallery/) and [github repo](https://github.com/martinaglv/touchTouch)
```haml
-= ttouch_image photo
+= touchgal_image photo
```
## Minimalistic Slideshow gallery
@@ -197,6 +197,8 @@ The RGallery should now also support multiple photo sources for responsive galle
```ruby
@photos.pages.add_photo_w_sources 'banner' => [{src: 'banner-HD', sizing: '2x'}, {src: 'banner-phone', sizing: '100w'}]
+Note: See module `RGallery::Pages` class.
+
# OR
@photos.pages.add_photo_sources 'banner' => [{src: 'banner-HD', sizing: '2x'}], 'logo' => [{src: 'logo-HD', sizing: '2x'}
@@ -212,7 +214,7 @@ The `RGallery::Photo` class is a base class for describing a photo.
You should create your own Photo class that inherits from `RGallery::Photo` (or implements the API), which knows how to render and describe your photos.
-Here a rough example:
+Here is a rough example:
```ruby
class Property
@@ -234,6 +236,13 @@ class Property
'responsive-gallery/images'
end
+ # The filename of the picture.
+ # Here it assumes that the id assigned is a Property object, which has a
+ # method 'picture' which returns the picture id.
+ def filename
+ "property-#{id.picture}"
+ end
+
def title
'property title'
end
@@ -249,7 +258,18 @@ class Property
end
```
-See the `lib/rails-gallery/rgallery/photos.rb
+See the `lib/rails-gallery/rgallery/photos.rb` for details on how to extend this class appropriately to fit your scenario.
+
+*debugging*
+
+In order to help debug the configuration of the photo class, you can use the view_helper methods:
+
+```ruby
+= validate_gallery_photo photo # prints error msg if invalid
+- validate_gallery_photo! photo # raise error if invalid
+```
+
+Or you can include the `RailsGallery::PhotoValidation` module anywhere you want to leverage these methods!
Then in your `properties/show.html.haml`:
View
2  VERSION
@@ -1 +1 @@
-0.2.2
+0.3.0
View
4 app/views/gallery/_galleria.html.haml
@@ -1,3 +1,3 @@
-#galleria
+#galleria.galleria
- photos.all.each do |photo|
- galleria_image photo
+ = gallery_image :galleria, photo
View
2  app/views/gallery/_responsive.html.haml
@@ -10,5 +10,5 @@
.es-carousel
%ul
- photos.all.each do |photo|
- = respgal_image photo, wrap: true
+ = gallery_image :responsive, photo, wrap: true
View
4 app/views/gallery/_slideshow.html.haml
@@ -13,12 +13,12 @@
.msg_thumb_wrapper
- photos.page(:first).photos.each do |photo|
%a{href: "#"}
- = ssgal_image photo
+ = gallery_image :slideshow photo
- photos.pages.remainder.each do |page|
.msg_thumb_wrapper{style: 'display:none'}
- page.photos.each do |photo|
%a{href: "#"}
- = ssgal_image photo
+ = gallery_image :slideshow, photo
%a#msg_thumb_next.msg_thumb_next{href: '#'}
%a#msg_thumb_prev.msg_thumb_prev{href: '#'}
%a#msg_thumb_close.msg_thumb_close{href: '#'}
View
1  lib/rails-gallery.rb
@@ -1,3 +1,4 @@
require 'rails-gallery/rgallery'
+require 'rails-gallery/photo_validation'
require 'rails-gallery/view_helper'
require 'rails-gallery/engine' if defined?(::Rails::Engine)
View
2  lib/rails-gallery/engine.rb
@@ -2,6 +2,8 @@ module RailsGallery
module Rails
class Engine < ::Rails::Engine
initializer 'rails gallery' do
+ puts "Adding RailsGallery::ViewHelper"
+
ActionView::Base.send :include, RailsGallery::ViewHelper
end
end
View
37 lib/rails-gallery/photo_validation.rb
@@ -0,0 +1,37 @@
+module RailsGallery
+ module PhotoValidation
+ def validate_gallery_photo photo
+ return "Photo must be a kind of RGallery::Photo, was: #{photo}" unless photo.kind_of?(RGallery::Photo)
+ return 'Photo must have a #path method' unless photo.respond_to? :path
+ return 'Photo must have a #title method' unless photo.respond_to? :title
+
+ begin
+ photo.filename
+ photo.file_path
+ rescue Exception => e
+ return "filename or file_path could not be resolved for: #{photo}, cause: #{e}"
+ end
+
+ return "Photo must have a path: #{photo}" if photo.path.blank?
+ return "Photo must have a title: #{photo}" if photo.title.blank?
+ true
+ end
+
+ def validate_gallery_photo! photo
+ raise ArgumentError, "Photo must be a kind of RGallery::Photo, was: #{photo}" unless photo.kind_of?(RGallery::Photo)
+ raise ArgumentError, 'Photo must have a #path method' unless photo.respond_to? :path
+ raise ArgumentError, 'Photo must have a #title method' unless photo.respond_to? :title
+
+ begin
+ photo.filename
+ photo.file_path
+ rescue Exception => e
+ raise ::RailsGallery::ConfigurationError, "filename or file_path could not be resolved for: #{photo}, cause: #{e}"
+ end
+
+ raise ArgumentError, "Photo must have a path: #{photo}" if photo.path.blank?
+ raise ArgumentError, "Photo must have a title: #{photo}" if photo.title.blank?
+ true
+ end
+ end
+end
View
15 lib/rails-gallery/view_helper.rb
@@ -1,9 +1,18 @@
module RailsGallery
+ class ConfigurationError < StandardError
+ end
+
module ViewHelper
+ include RailsGallery::PhotoValidation
+
def self.galleries
%w{galleria responsive slideshow touch_touch}
end
+ def self.version
+ '0.2.2'
+ end
+
# autoload all galleries when references
galleries.each do |gallery|
autoload gallery.camelize.to_sym, "rails-gallery/view_helper/#{gallery}"
@@ -11,18 +20,20 @@ def self.galleries
def gallery_image type, photo
meth_name = "#{type}_gallery_image"
+ validate_gallery_photo! photo
unless respond_to? meth_name
raise ArgumentError, "Gallery #{type} is not yet supported. Please add a View helper module for this gallery using the convention followed by the other galleries..."
end
send(meth_name, photo)
end
- def gallery_imageset type, photo
+ def gallery_imageset type, imageset
meth_name = "#{type}_gallery_imageset"
+ # validate_gallery_imageset! imageset
unless respond_to? meth_name
raise ArgumentError, "Gallery #{type} is not yet supported for imageset. Please add a View helper module for this gallery using the convention followed by the other galleries..."
end
- send(meth_name, photo)
+ send(meth_name, imageset)
end
protected
View
120 spec/rails-gallery/view_helper_spec.rb
@@ -0,0 +1,120 @@
+require 'spec_helper'
+
+class NoPathPhoto < RGallery::Photo
+ def path
+ nil
+ end
+end
+
+class BadFilenamePhoto < RGallery::Photo
+ def title
+ 'my title'
+ end
+
+ # blip method unknown!
+ def filename
+ blip
+ end
+end
+
+class NoTitlePhoto < RGallery::Photo
+ def path
+ 'abc'
+ end
+
+ def title
+ nil
+ end
+end
+
+class ValidPhoto < RGallery::Photo
+ def path
+ 'abc'
+ end
+
+ def title
+ 'sdgds'
+ end
+end
+
+describe RailsGallery::ConfigurationError do
+ specify { RailsGallery::ConfigurationError.new.should be_a StandardError }
+end
+
+describe RailsGallery::ViewHelper do
+ include ControllerTestHelpers,
+ RailsGallery::ViewHelper
+
+ let(:no_path_photo) { NoPathPhoto.new 1 }
+ let(:no_title_photo) { NoTitlePhoto.new 2 }
+ let(:bad_filename_photo) { BadFilenamePhoto.new 2 }
+ let(:valid_photo) { ValidPhoto.new 2 }
+
+ describe 'validate_gallery_photo! photo' do
+ it 'should raise error on nil' do
+ expect { validate_gallery_photo!(nil) }.to raise_error(ArgumentError)
+ end
+
+ it 'should raise error on no #path method' do
+ expect { validate_gallery_photo!(no_path_photo) }.to raise_error(ArgumentError)
+ end
+
+ it 'should raise error on no #title method' do
+ expect { validate_gallery_photo!(no_title_photo) }.to raise_error(ArgumentError)
+ end
+
+ it 'should raise error on bad filename method' do
+ expect { validate_gallery_photo!(bad_filename_photo) }.to raise_error(::RailsGallery::ConfigurationError)
+ end
+ end
+
+ describe 'validate_gallery_photo photo' do
+ it 'should return nil error msg' do
+ validate_gallery_photo(nil).should == "Photo must be a kind of RGallery::Photo, was: "
+ end
+
+ it 'should return no path error msg' do
+ validate_gallery_photo(no_path_photo).should match /Photo must have a path:/
+ end
+
+ it 'should return no title error msg' do
+ validate_gallery_photo(no_title_photo).should match /Photo must have a title:/
+ end
+
+ it 'should return bad filename method error' do
+ validate_gallery_photo(bad_filename_photo).should match /filename or file_path could not be resolved for:/
+ end
+ end
+
+ describe 'gallery_image photo' do
+ it 'should raise error on nil' do
+ expect { gallery_image(nil) }.to raise_error(ArgumentError)
+ end
+
+ it 'should return error if valid type but nil photo' do
+ expect { gallery_image(:galleria, nil) }.to raise_error(ArgumentError)
+ end
+
+ it 'should return error if invalid type and valid photo' do
+ expect { gallery_image(:invalid, valid_photo) }.to raise_error(ArgumentError)
+ end
+
+ it "should no raise error for galleria image" do
+ expect { gallery_image(:galleria, valid_photo) }.to_not raise_error
+ end
+
+ it "should render the image for galleria image" do
+ gallery_image(:galleria, valid_photo).should == "<a href=\"abc\"><img data-description=\"no description\" data-title=\"sdgds\" src=\"abc\"></img></a>"
+ end
+
+ %w{slideshow responsive}.each do |gallery_type|
+ it "should not raise error for type: #{gallery_type}" do
+ expect { gallery_image(gallery_type, valid_photo) }.to_not raise_error
+ end
+
+ it "should render the image for galleria image" do
+ gallery_image(gallery_type, valid_photo).should match /img.*alt=.*src="\w+"/
+ end
+ end
+ end
+end
View
44 spec/spec_helper.rb
@@ -1,6 +1,46 @@
-require 'rspec'
+# coding: utf-8
+require 'rubygems'
require 'rails'
+require 'json'
+require 'active_support'
+require 'action_pack'
+require 'action_view'
+require 'action_controller'
+require 'action_view/template'
+
+require 'rspec'
require 'rails-gallery'
-RSpec.configure do |config|
+module ControllerTestHelpers
+
+ def self.included(base)
+ base.class_eval do
+
+ include ActionView::Helpers,
+ ActionView::Helpers::CaptureHelper,
+ ActionView::Helpers::JavaScriptHelper,
+ ActionView::Helpers::AssetTagHelper
+
+ # allow tabs.create to run by stubbing an output_buffer
+ attr_accessor :output_buffer
+ @output_buffer = ""
+
+ # stub content_for for testing
+ def content_for(name, content = nil, &block)
+ # this doesn't exist, and causes errors
+ @_content_for = {} unless defined? @_content_for
+ # we've got to initialize this, so we can concat to it
+ @_content_for[name] = '' if @_content_for[name].nil?
+ # now the rest is the same as in rails
+ content = capture(&block) if block_given?
+ @_content_for[name] << content if content
+ @_content_for[name] unless content
+ end
+
+ def image_tag src, options = {}
+ content_tag :img, nil, options.merge(src: src)
+ end
+ end
+ end
+
end
View
5,926 vendor/assets/javascripts/gallery/galleria.js
5,926 additions, 0 deletions not shown
View
0  vendor/assets/stylesheets/touch_touch.css → vendor/assets/stylesheets/gallery/touch_touch.css
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.