Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added presenter method attr_expose to AR #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Say, for example, you have a basic Food model:
```ruby
class Food < ActiveRecord::Base
include RocketPants::Cacheable

# Only expose the following attributes in the API JSON response
attr_expose :name, :calories
end
```

Expand Down
3 changes: 3 additions & 0 deletions lib/rocket_pants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ module RocketPants
autoload :Cacheable, 'rocket_pants/cacheable'
autoload :CacheMiddleware, 'rocket_pants/cache_middleware'

# Model
autoload :Presenter, 'rocket_pants/presenter'

# Helpers for various testing frameworks.
autoload :TestHelper, 'rocket_pants/test_helper'
autoload :RSpecMatchers, 'rocket_pants/rspec_matchers'
Expand Down
1 change: 1 addition & 0 deletions lib/rocket_pants/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Base < ActionController::Metal
ActionController::Rescue,
ErrorHandling,
Rescuable,
Presenter,
JSONP
# FormatVerification # TODO: Implement Format Verification
].compact
Expand Down
30 changes: 30 additions & 0 deletions lib/rocket_pants/presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Provides a way of selecting certain field attributes to present in the JSON response
module RocketPants
module Presenter
extend ActiveSupport::Concern

included do
class_attribute :_presented_attributes
end

module ClassMethods
# Accepts a list of attributes to be presented in the response
#
def attr_expose(*args)
options = args.extract_options!
self._presented_attributes = (self._presented_attributes || []) + args
end
end

# Override to only show model attributes marked for presentation with attr_presented
def serializable_hash(options = {})
options[:only] = (options[:only] || []) | (self.class._presented_attributes || [])
options[:only] = nil if options[:only].empty?
super(options)
end

ActiveSupport.on_load :active_record do
ActiveRecord::Base.send :include, RocketPants::Presenter
end
end
end
21 changes: 21 additions & 0 deletions spec/rocket_pants/presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'
require 'rocket_pants/presenter'

describe RocketPants::Presenter do
include ControllerHelpers

use_reversible_tables :fish, :scope => :all

it 'should provide an attr_exposed method on an AR model' do
Fish.respond_to?(:attr_expose).should be_true
end

it 'should only expose certain attributes' do
attrs = {:token => "a", :name => "Test Fish", :latin_name => "Latin Name", :child_number => 5}

Fish.attr_expose :name

f = Fish.create! attrs
f.serializable_hash.keys.should == ["name"]
end
end