attr_encodable 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -0
- data/lib/encodable/active_record/class_methods.rb +4 -0
- data/spec/attr_encodable_spec.rb +7 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -169,6 +169,13 @@ User.attr_encodable :login, :admin, :email, :password, :as => :admin_api
|
|
169
169
|
|
170
170
|
Now you can call `@user.to_json(:admin_api)` somewhere, which will include a full users' details, but any other `as_json` call will keep that information private.
|
171
171
|
|
172
|
+
#### Scopes
|
173
|
+
|
174
|
+
The use of `:as` also creates a scope on the class which is a SELECT limited only to those columns the class knows about. This enables higher-performance API calls out-of-the-box.
|
175
|
+
|
176
|
+
Using the first example from above, calling `User.listing` would result in a `SELECT login FROM users` instead of the normal `SELECT * FROM users`. Since
|
177
|
+
you're only going to be encoding the information from attr_encodable anyway, there's no sense in selecting anything else!
|
178
|
+
|
172
179
|
Okay, that's all. Thanks for stopping by.
|
173
180
|
|
174
181
|
Copyright © 2011 Flip Sasser
|
@@ -19,6 +19,10 @@ module Encodable
|
|
19
19
|
add_encodable_attribute(attribute, attribute, options)
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
if options[:as] != :default
|
24
|
+
scope options[:as], select(default_attributes(options[:as]).reject{|attribute| !column_names.include?(attribute.to_s)})
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
28
|
def add_encodable_attribute(method, value, options = {})
|
data/spec/attr_encodable_spec.rb
CHANGED
@@ -217,5 +217,12 @@ describe Encodable do
|
|
217
217
|
@user.as_json.should == @user.attributes
|
218
218
|
@user.as_json(:short).should == {'id' => 1, 'first_name' => 'flip', 'last_name' => 'sasser'}
|
219
219
|
end
|
220
|
+
|
221
|
+
it "should also create a named_scope that limits the SELECT statement to the included attributes" do
|
222
|
+
User.attr_encodable :id, :as => :short
|
223
|
+
User.first.first_name.should == 'flip'
|
224
|
+
lambda { User.short.first.first_name }.should raise_error(ActiveModel::MissingAttributeError)
|
225
|
+
User.short.first.id.should == 1
|
226
|
+
end
|
220
227
|
end
|
221
228
|
end
|