Skip to content

Commit

Permalink
added ControllerAdditions for easier reponder handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
apotonick committed Mar 5, 2012
1 parent 8d3dcc6 commit accaa41
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
12 changes: 2 additions & 10 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,27 @@ Easily render resources using representers with the built-in responder.

```ruby
class SingersController < ApplicationController
include Roar::Rails::ControllerAdditions
respond_to :json

def show
singer = Singer.find_by_id(params[:id])
respond_with singer
end

def self.responder
Class.new(super).send :include, Roar::Rails::Responder
end

end
```

Need to use a representer with a different name than your model? Pass it in using the `:with_representer` option:

```ruby
class SingersController < ApplicationController
include Roar::Rails::ControllerAdditions
respond_to :json

def show
singer = Musician.find_by_id(params[:id])
respond_with singer, :with_representer => SingerRepresenter
end

def self.responder
Class.new(super).send :include, Roar::Rails::Responder
end

end
```

Expand Down
2 changes: 1 addition & 1 deletion lib/roar-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Feature

module Roar
module Rails
# Your code goes here...
autoload("TestCase", "roar/rails/test_case")
autoload("ControllerAdditions", "roar/rails/controller_additions")
end
end
11 changes: 11 additions & 0 deletions lib/roar/rails/controller_additions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Roar::Rails
module ControllerAdditions
extend ActiveSupport::Concern

module ClassMethods
def responder
Class.new(super).send :include, Roar::Rails::Responder
end
end
end
end
4 changes: 3 additions & 1 deletion lib/roar/rails/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def extend_with_representer!(resource, representer=nil)
representer ||= representer_for_resource(resource)
resource.extend(representer)
end

def display(resource, given_options={})
if resource.respond_to?(:map!)
resource.map! do |r|
Expand All @@ -15,7 +16,8 @@ def display(resource, given_options={})
end
super
end
private

private
def representer_for_resource(resource)
(resource.class.name + "Representer").constantize
end
Expand Down
6 changes: 1 addition & 5 deletions test/responder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Singer = Struct.new(:name)
class SingersController < ActionController::Base
include Roar::Rails::ControllerAdditions
respond_to :json

def explicit_representer
Expand All @@ -18,11 +19,6 @@ def collection_of_representers
singers = [Singer.new("Bumi"), Singer.new("Bjork"), Singer.new("Sinead")]
respond_with singers
end

def self.responder
Class.new(super).send :include, Roar::Rails::Responder
end

end

class ResponderTest < ActionController::TestCase
Expand Down

0 comments on commit accaa41

Please sign in to comment.