Skip to content
Browse files

split photos model into files and group under RGallery namespace

  • Loading branch information...
1 parent adc1caf commit 46d756fd0cc324483ab891322dbf6bbe80898848 @kristianmandrup committed Aug 22, 2012
View
86 README.md
@@ -12,7 +12,7 @@ Popular Javascript Photo galleries/carousels ready to use with Rails 3+.
* responsive
* galleria
-Please add more ;)
+Please add more using a similar convention as is used for these galleries ;)
## Configuration
@@ -109,9 +109,9 @@ You just have to change 5000 to the value you want (milliseconds).
See [galleria.io](http://galleria.io) for more info.
-## Extras
+## Rails engine usage
-This gem is a Rails 3+ engine.
+This gem is a Rails 3+ engine :)
Some *HAML* views (partials) are included in `app/views/gallery`
@@ -137,7 +137,7 @@ class PropertiesController < ApplicationController
end
def photos
- @photos ||= Photos.new nil, photo_class: Property::Photo
+ @photos ||= RGallery::Photos.new nil, photo_class: Property::Photo
5.times do
@photos.pages << 6.times.map {|n| (Kernel.rand(7) + 1).to_s }
end
@@ -146,6 +146,50 @@ class PropertiesController < ApplicationController
end
```
+This engine comes with a RGallery::Photos` model which can encapsulate your photos for display and allows you to group photos in multiple pages.
+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:
+
+```ruby
+class Property
+ class Photo < RGallery::Photo
+ def path
+ File.join folder, super
+ end
+
+ # mogrify -path fullpathto/temp2 -resize 60x60% -quality 60 -format jpg *.png
+
+ # this will take all png files in your current directory (temp),
+ # resize to 60% (of largest dimension and keep aspect ratio),
+ # set jpg quality to 60 and convert to jpg.
+ def thumb
+ File.join folder, 'thumbs', file_path
+ end
+
+ def folder
+ 'responsive-gallery/images'
+ end
+
+ def title
+ 'property title'
+ end
+
+ def alt
+ 'property alt'
+ end
+
+ def self.extension
+ :jpg
+ end
+ end
+end
+```
+
+See the `lib/rails-gallery/rgallery/photos.rb
+
Then in your `properties/show.html.haml`:
```haml
@@ -160,15 +204,45 @@ There are also some view helpers included in `rails-gallery/view_helper.rb`
`gallery_image type, photo`
-Example:
+Simple example:
+
+Iterate all photos as a "single page".
```haml
-- photos.each do |photo|
+- photos.all.each do |photo|
= gallery_image :responsive, photo`
```
+Pages example:
+
+Iterate photos, one page at a time.
+
+```haml
+- photos.pages.each do |photo|
+ = gallery_image :responsive, photo`
+```
+
+Advanced example:
+
+Iterate photos, first page visible, then remaining pages invisible.
+
+```haml
+.page.visible
+ - photos.page(:first).photos.each do |photo|
+ = gallery_image :responsive, photo`
+
+- photos.pages.remainder.each do |page|
+ .page.hidden
+ - page.photos.each do |photo|
+ = gallery_image :responsive, photo`
+```
+
Enjoy!
+## TODO
+
+Would perhaps be nice to allow pages/albums to have info assigned, such as title and/or description, tags etc. ?
+
## Contributing to rails-gallery
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
View
2 lib/rails-gallery.rb
@@ -1 +1 @@
-require 'rails-gallery/photos'
+require 'rails-gallery/rgallery'
View
14 lib/rails-gallery/photo_config.rb
@@ -1,14 +0,0 @@
-class PhotoConfig
- attr_writer :photo_class
- attr_reader :options
-
- def initialize options = {}
- options ||= {}
- @options = options
- @photo_class = options[:photo_class] if options[:photo_class]
- end
-
- def photo_class
- @photo_class ||= Photos::Photo
- end
-end
View
139 lib/rails-gallery/photos.rb
@@ -1,139 +0,0 @@
-require 'enumerator'
-require 'rails-gallery/photo_config'
-
-class Photos < PhotoConfig
- def initialize pages = nil, options = {}
- unless pages.nil?
- raise ArgumentError, "Must be a Photos::Pages or Array, was: #{pages}" unless valid_pages? pages
- pages = Pages.new pages, options if pages.kind_of?(Array)
- @pages = pages
- end
- super options
- end
-
- def all
- pages.inject([]) {|res, page| res += page.photos }.flatten.compact
- end
-
- def pages
- @pages ||= Pages.new nil, options
- end
-
- def page id
- raise ArgumentError, "Page id must be one #{valid_page_ids}, was: #{id}" unless valid_page_id? id
- pages.send(id)
- end
-
- def valid_page_id? id
- [:first].include? id.to_sym
- end
-
- def valid_page_ids
- [:first]
- end
-
- def valid_pages? pages
- pages.kind_of?(Photos::Pages) || pages.kind_of?(Array)
- end
-
- class Pages < PhotoConfig
- include Enumerable
-
- def initialize photo_list = [], options = {}
- super options
- self.send(:<<, photo_list) unless photo_list.blank?
- end
-
- def each &block
- pages.each {|page| yield page }
- end
-
- def remainder
- pages[1..-1] || []
- end
-
- def first
- pages.first || []
- end
-
- def << photo_list
- pages << Page.new(photo_list, options)
- end
-
- delegate :empty?, to: :pages
-
- protected
-
- def pages
- @pages ||= []
- end
- end
-
- class Page < PhotoConfig
- include Enumerable
-
- def initialize photo_ids = [], options = {}
- @photo_ids = photo_ids
- super options
- end
-
- def photo_ids
- @photo_ids ||= []
- end
-
- def photos
- @photos ||= photo_ids.map {|id| photo_class.new id, options }
- end
-
- delegate :empty?, to: :photos
-
- def each &block
- photos.each {|photo| yield photo }
- end
- end
-
- class Photo
- attr_reader :id, :options
-
- def initialize id, options = {}
- @id = id
- @options = options
- end
-
- def file_path
- "#{id}.#{extension}"
- end
-
- def path
- file_path
- end
-
- def thumb
- path
- end
-
- def title
- 'no title'
- end
-
- def alt
- 'no alt'
- end
-
- def description
- 'no description'
- end
-
- def extension
- options[:extension] || self.class.extension
- end
-
- class << self
- attr_writer :extension
-
- def extension
- @extension ||= :png
- end
- end
- end
-end
View
7 lib/rails-gallery/rgallery.rb
@@ -0,0 +1,7 @@
+module RGallery
+ autoload :Photos, 'rails-gallery/rgallery/photos'
+ autoload :Pages, 'rails-gallery/rgallery/pages'
+ autoload :Page, 'rails-gallery/rgallery/page'
+ autoload :Photo, 'rails-gallery/rgallery/photo'
+ autoload :PhotoConfig, 'rails-gallery/rgallery/photo_config'
+end
View
24 lib/rails-gallery/rgallery/page.rb
@@ -0,0 +1,24 @@
+module RGallery
+ class Page < PhotoConfig
+ include Enumerable
+
+ def initialize photo_ids = [], options = {}
+ @photo_ids = photo_ids
+ super options
+ end
+
+ def photo_ids
+ @photo_ids ||= []
+ end
+
+ def photos
+ @photos ||= photo_ids.map {|id| photo_class.new id, options }
+ end
+
+ delegate :empty?, to: :photos
+
+ def each &block
+ photos.each {|photo| yield photo }
+ end
+ end
+end
View
34 lib/rails-gallery/rgallery/pages.rb
@@ -0,0 +1,34 @@
+module RGallery
+ class Pages < PhotoConfig
+ include Enumerable
+
+ def initialize photo_list = [], options = {}
+ super options
+ self.send(:<<, photo_list) unless photo_list.blank?
+ end
+
+ def each &block
+ pages.each {|page| yield page }
+ end
+
+ def remainder
+ pages[1..-1] || []
+ end
+
+ def first
+ pages.first || []
+ end
+
+ def << photo_list
+ pages << RGallery::Page.new(photo_list, options)
+ end
+
+ delegate :empty?, to: :pages
+
+ protected
+
+ def pages
+ @pages ||= []
+ end
+ end
+end
View
50 lib/rails-gallery/rgallery/photo.rb
@@ -0,0 +1,50 @@
+module RGallery
+ class Photo
+ attr_reader :id, :options
+
+ def initialize id, options = {}
+ @id = id
+ @options = options
+ end
+
+ def filename
+ id
+ end
+
+ def file_path
+ "#{filename}.#{extension}"
+ end
+
+ def path
+ file_path
+ end
+
+ def thumb
+ path
+ end
+
+ def title
+ 'no title'
+ end
+
+ def alt
+ 'no alt'
+ end
+
+ def description
+ 'no description'
+ end
+
+ def extension
+ options[:extension] || self.class.extension
+ end
+
+ class << self
+ attr_writer :extension
+
+ def extension
+ @extension ||= :png
+ end
+ end
+ end
+end
View
18 lib/rails-gallery/rgallery/photo_config.rb
@@ -0,0 +1,18 @@
+module RGallery
+ class PhotoConfig
+ attr_writer :photo_class
+ attr_reader :options
+
+ def initialize options = {}
+ options ||= {}
+ @options = options
+ @photo_class = options[:photo_class] if options[:photo_class]
+ end
+
+ protected
+
+ def photo_class
+ @photo_class ||= Photos::Photo
+ end
+ end
+end
View
46 lib/rails-gallery/rgallery/photos.rb
@@ -0,0 +1,46 @@
+require 'enumerator'
+require 'rails-gallery/photo_config'
+
+module RGallery
+ class Photos < PhotoConfig
+ def initialize pages = nil, options = {}
+ unless pages.nil?
+ raise ArgumentError, "Must be a Photos::Pages or Array, was: #{pages}" unless valid_pages? pages
+ pages = pages_class.new pages, options if pages.kind_of?(Array)
+ @pages = pages
+ end
+ super options
+ end
+
+ def all
+ pages.inject([]) {|res, page| res += page.photos }.flatten.compact
+ end
+
+ def pages
+ @pages ||= pages_class.new nil, options
+ end
+
+ def page id
+ raise ArgumentError, "Page id must be one #{valid_page_ids}, was: #{id}" unless valid_page_id? id
+ pages.send(id)
+ end
+
+ protected
+
+ def valid_page_id? id
+ [:first].include? id.to_sym
+ end
+
+ def valid_page_ids
+ [:first]
+ end
+
+ def valid_pages? pages
+ pages.kind_of?(pages_class) || pages.kind_of?(Array)
+ end
+
+ def pages_class
+ RGallery::Pages
+ end
+ end
+end

0 comments on commit 46d756f

Please sign in to comment.
Something went wrong with that request. Please try again.