Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Provides a filter for assigning attributes to a new resource #40

Closed
wants to merge 3 commits into from

3 participants

@coffeencoke

The Need:

  1. A way to allow assignment for specific attributes when creating and updating a resource.
  2. A way to initialize a resource with all attributes, including attributes disallowed in the first need.

This pull request provides a filter for assigning attributes to a resource. This would also resolve #34.

@voxdolo
Collaborator

@coffeencoke As mentioned in #34, I'm good with this pull request, but I'd like to see the spec call out the added functionality, so it doesn't get lost in the translation to the new version of DE. thanks!

@jgdavey
Owner

Although the commits did not apply directly, in the version of DecentExposure on master (and soon to be 2.0), we do address the issue there

Therefore, I'm closing this issue.

@jgdavey jgdavey closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 16, 2012
  1. @coffeencoke

    Assign attributes via attributes= so that it can be used as a filter …

    coffeencoke authored
    …to specify assignments outside of the initialize
  2. @coffeencoke
Commits on Apr 3, 2012
This page is out of date. Refresh to see the latest.
View
12 lib/decent_exposure/default_exposure.rb
@@ -15,12 +15,14 @@ def self.included(klass)
proxy = name.to_s.classify.constantize
end
- if id = params["#{name}_id"] || params[:id]
- proxy.find(id).tap do |r|
- r.attributes = params[name] unless request.get?
- end
+ resource = if id = params["#{name}_id"] || params[:id]
+ proxy.find(id)
else
- proxy.new(params[name])
+ proxy.new
+ end
+
+ resource.tap do |r|
+ r.attributes = params[name] if r && params[name] && !request.get?
end
end
end
View
12 spec/lib/rails_integration_spec.rb
@@ -7,12 +7,14 @@ class Resource
def self.scoped(opts); self; end
def self.find(*args); end
def initialize(*args); end
+ def attributes=(*args); end
end
class Equipment
def self.scoped(opts); self; end
def self.find(*args); end
def initialize(*args); end
+ def attributes=(*args); end
end
describe "Rails' integration:", DecentExposure do
@@ -100,8 +102,9 @@ def initialize(*args); end
before{ controller.expose(:person) }
it 'uses the existing collection method' do
+ person = Person.new
instance.stubs(:people).returns(collection)
- collection.expects(:new)
+ collection.expects(:new).returns(person)
instance.person
end
end
@@ -111,8 +114,9 @@ def initialize(*args); end
before{ controller.expose(:person) }
it 'falls back to the singularized constant' do
+ person = Person.new
instance.stubs(:people).returns(collection)
- Person.expects(:new)
+ Person.expects(:new).returns(person)
instance.person
end
end
@@ -139,7 +143,9 @@ def initialize(*args); end
end
it 'calls new with params[:resouce_name]' do
- Resource.expects(:new).with({:name => 'bob'})
+ resource = Resource.new
+ Resource.expects(:new).returns(resource)
+ resource.expects(:attributes=).with({:name => 'bob'})
instance.resource
end
end
Something went wrong with that request. Please try again.