Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added has_feed resources helper
  • Loading branch information
Alexander Semyonov committed Mar 24, 2010
1 parent 972bdee commit 17b6c52
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
3 changes: 2 additions & 1 deletion espresso.gemspec
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Alexander Semyonov"]
s.date = %q{2010-03-23}
s.date = %q{2010-03-24}
s.description = %q{Useful templates for controller and model functions}
s.email = %q{rotuka@tokak.ru}
s.extra_rdoc_files = [
Expand All @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
"espresso.gemspec",
"init.rb",
"lib/espresso.rb",
"lib/espresso/action_controller.rb",
"lib/espresso/action_view.rb",
"lib/espresso/action_view/form_builder.rb",
"lib/espresso/action_view/navigation.rb",
Expand Down
2 changes: 2 additions & 0 deletions lib/espresso.rb
Expand Up @@ -4,6 +4,7 @@ module Espresso
autoload :Model, 'espresso/model'
autoload :Resources, 'espresso/resources'
autoload :ActionView, 'espresso/action_view'
autoload :ActionController, 'espresso/action_controller'
autoload :ResourcesHelpers, 'espresso/resources_helpers'

# Make a list of locale files, provided by gem
Expand All @@ -18,6 +19,7 @@ def self.locale_files

if defined?(ActionController)
ActionController::Base.send(:include, Espresso::ResourcesHelpers)
ActionController::Base.extend(Espresso::ActionController)
end

if defined?(ActionView)
Expand Down
7 changes: 7 additions & 0 deletions lib/espresso/action_controller.rb
@@ -0,0 +1,7 @@
module Espresso
module ActionController
def resources
Espresso::Resources.resources(self)
end
end
end
8 changes: 6 additions & 2 deletions lib/espresso/model.rb
Expand Up @@ -4,19 +4,23 @@
module Espresso
# @author Alexander Semyonov
module Model
unloadable

def self.included(model)
super
model.class_eval do
extend ClassMethods

class_inheritable_accessor :model_attrs, :name_field, :per_page

self.model_attrs = []
self.name_field = :name
self.per_page = 10

named_scope :for_index, :conditions => '1 = 1'
named_scope :for_feed, :conditions => '1 = 1'
named_scope :for_show, :conditions => '1 = 1'
named_scope :recent, lambda {
{:limit => per_page}
}
end
end

Expand Down
45 changes: 44 additions & 1 deletion lib/espresso/resources.rb
Expand Up @@ -2,11 +2,54 @@

module Espresso
module Resources
def self.included(controller)
def self.resources(controller = nil)
controller ||= self
controller.inherit_resources
controller.extend ClassMethods
controller.send(:include, InstanceMethods)
end

def self.included(controller)
resources(controller)
end

module ClassMethods
# Adds ability to build feeds for resource’s collections.
# By default adds Atom feed
# @example Add Atom feed for #index
# has_feed :atom
# @example Add Atom and RSS feed for #index
# has_feed :atom, :rss
# @example Add Atom, Rss, and ITunes feed for #index, #list and #archive
# has_feed :atom, :rss, :itunes_feed, :only => [:index, :list, :archive]
def has_feed(*args)
options = args.extract_options!
if options.empty?
options[:only] = :index
end

class_inheritable_accessor :feed_formats
self.feed_formats = args.empty? ? [:atom] : args
self.feed_formats = self.feed_formats.map(&:to_sym)

self.feed_formats.each do |format|
respond_to format, options
end

include FeedScope
end
end

module FeedScope
def apply_scopes(target_object)
if params[:format] && self.class.feed_formats.include?(params[:format].to_sym)
target_object.for_feed
else
target_object
end
end
end

module InstanceMethods
protected

Expand Down

0 comments on commit 17b6c52

Please sign in to comment.