attr_encodable 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,11 @@ module Encodable
3
3
  module ClassMethods
4
4
  def attr_encodable(*attributes)
5
5
  options = extract_encodable_options!(attributes)
6
-
7
- unless @encodable_whitelist_started
6
+ unless @encodable_whitelist_started && @encodable_whitelist_started.key?(options[:as])
8
7
  # Since we're white-listing, make sure we black-list every attribute to begin with
9
8
  unencodable_attributes(options[:as]).push *column_names.map(&:to_sym)
10
- @encodable_whitelist_started = true
9
+ @encodable_whitelist_started ||= {}
10
+ @encodable_whitelist_started[options[:as]] = true
11
11
  end
12
12
 
13
13
  attributes.each do |attribute|
@@ -21,7 +21,8 @@ module Encodable
21
21
  end
22
22
 
23
23
  if options[:as] != :default
24
- scope options[:as], select(default_attributes(options[:as]).reject{|attribute| !column_names.include?(attribute.to_s)})
24
+ columns = default_attributes(options[:as]).reject{|attribute| !column_names.include?(attribute.to_s)}
25
+ scope options[:as], select(columns.map {|column| "#{table_name}.#{column}" })
25
26
  end
26
27
  end
27
28
 
@@ -74,7 +75,7 @@ module Encodable
74
75
  private
75
76
  def extract_encodable_options!(attributes)
76
77
  begin
77
- attributes.last.assert_valid_keys(:prefix, :as)
78
+ attributes.last.assert_valid_keys(:as, :prefix)
78
79
  options = attributes.extract_options!
79
80
  rescue ArgumentError
80
81
  end if attributes.last.is_a?(Hash)
@@ -68,31 +68,31 @@ describe Encodable do
68
68
  User.attr_encodable :id, :first_name
69
69
  User.unencodable_attributes(:default).map(&:to_s).should == ['foo', 'bar', 'baz'] + User.column_names - ['id', 'first_name']
70
70
  end
71
-
71
+
72
72
  describe "at the parent model level" do
73
73
  it "should not mess with to_json unless when attr_encodable and attr_unencodable are not set" do
74
74
  @user.as_json.should == @user.attributes
75
75
  end
76
-
76
+
77
77
  it "should not mess with :include options" do
78
78
  @user.as_json(:include => :permissions).should == @user.attributes.merge(:permissions => @user.permissions.as_json)
79
79
  end
80
-
80
+
81
81
  it "should not mess with :methods options" do
82
82
  @user.as_json(:methods => :foobar).should == @user.attributes.merge(:foobar => "baz")
83
83
  end
84
-
84
+
85
85
  it "should allow me to whitelist attributes" do
86
86
  User.attr_encodable :login, :first_name, :last_name
87
87
  @user.as_json.should == @user.attributes.slice('login', 'first_name', 'last_name')
88
88
  end
89
-
89
+
90
90
  it "should allow me to blacklist attributes" do
91
91
  User.attr_unencodable :login, :first_name, :last_name
92
92
  @user.as_json.should == @user.attributes.except('login', 'first_name', 'last_name')
93
93
  end
94
-
95
-
94
+
95
+
96
96
  # Of note is the INSANITY of ActiveRecord in that it applies :only / :except to :include as well. Which is
97
97
  # obviously insane. Similarly, it doesn't allow :methods to come along when :only is specified. Good god, what
98
98
  # a shame.
@@ -100,33 +100,33 @@ describe Encodable do
100
100
  User.attr_encodable :login, :first_name, :last_name
101
101
  @user.as_json(:include => :permissions).should == @user.attributes.slice('login', 'first_name', 'last_name').merge(:permissions => @user.permissions.as_json)
102
102
  end
103
-
103
+
104
104
  it "should allow me to blacklist attributes without messing with :include and :methods" do
105
105
  User.attr_unencodable :login, :first_name, :last_name
106
106
  @user.as_json(:include => :permissions, :methods => :foobar).should == @user.attributes.except('login', 'first_name', 'last_name').merge(:permissions => @user.permissions.as_json, :foobar => "baz")
107
107
  end
108
-
108
+
109
109
  it "should not screw with :include if it's a hash" do
110
110
  User.attr_unencodable :login, :first_name, :last_name
111
111
  @user.as_json(:include => {:permissions => {:methods => :hello, :except => :id}}, :methods => :foobar).should == @user.attributes.except('login', 'first_name', 'last_name').merge(:permissions => @user.permissions.as_json(:methods => :hello, :except => :id), :foobar => "baz")
112
112
  end
113
113
  end
114
-
114
+
115
115
  describe "at the child model level when the parent model has attr_encodable set" do
116
116
  before :each do
117
117
  User.attr_encodable :login, :first_name, :last_name
118
118
  end
119
-
119
+
120
120
  it "should not mess with to_json unless when attr_encodable and attr_unencodable are not set on the child, but are on the parent" do
121
121
  @user.permissions.as_json.should == @user.permissions.map(&:attributes)
122
122
  end
123
-
123
+
124
124
  it "should not mess with :include options" do
125
125
  # This is testing that the implicit ban on the :id attribute from User.attr_encodable is not
126
126
  # applying to serialization of permissions
127
127
  @user.as_json(:include => :permissions)[:permissions].first['id'].should_not be_nil
128
128
  end
129
-
129
+
130
130
  it "should inherit any attr_encodable options from the child model" do
131
131
  User.attr_encodable :id
132
132
  Permission.attr_encodable :name
@@ -145,33 +145,33 @@ describe Encodable do
145
145
  # @user.as_json.should == @user.attributes.except('login', 'first_name', 'last_name')
146
146
  # end
147
147
  end
148
-
148
+
149
149
  it "should let me specify automatic includes as well as attributes" do
150
150
  User.attr_encodable :login, :first_name, :id, :permissions
151
151
  @user.as_json.should == @user.attributes.slice('login', 'first_name', 'id').merge(:permissions => @user.permissions.as_json)
152
152
  end
153
-
153
+
154
154
  it "should let me specify methods as well as attributes" do
155
155
  User.attr_encodable :login, :first_name, :id, :foobar
156
156
  @user.as_json.should == @user.attributes.slice('login', 'first_name', 'id').merge(:foobar => "baz")
157
157
  end
158
-
158
+
159
159
  it "should allow me to only request certain whitelisted attributes and methods" do
160
160
  User.attr_encodable :login, :first_name, :last_name, :foobar
161
161
  @user.as_json(:only => [:login, :foobar]).should == {'login' => 'flipsasser', :foobar => 'baz'}
162
162
  end
163
-
163
+
164
164
  it "should allow me to use :only with aliased methods and attributes" do
165
165
  User.attr_encodable :login => :login_eh, :first_name => :foist, :last_name => :last, :foobar => :baz
166
166
  @user.as_json(:only => [:login, :foobar]).should == {'login_eh' => 'flipsasser', 'baz' => 'baz'}
167
167
  end
168
-
168
+
169
169
  describe "reassigning" do
170
170
  it "should let me reassign attributes" do
171
171
  User.attr_encodable :id => :identifier
172
172
  @user.as_json.should == {'identifier' => @user.id}
173
173
  end
174
-
174
+
175
175
  it "should let me reassign attributes alongside regular attributes" do
176
176
  User.attr_encodable :login, :last_name, :id => :identifier
177
177
  @user.as_json.should == {'identifier' => 1, 'login' => 'flipsasser', 'last_name' => 'sasser'}
@@ -181,29 +181,29 @@ describe Encodable do
181
181
  User.attr_encodable :id => :identifier, :first_name => :foobar
182
182
  @user.as_json.should == {'identifier' => 1, 'foobar' => 'flip'}
183
183
  end
184
-
184
+
185
185
  it "should let me reassign :methods" do
186
186
  User.attr_encodable :foobar => :w00t
187
187
  @user.as_json.should == {'w00t' => 'baz'}
188
188
  end
189
-
189
+
190
190
  it "should let me reassign :include" do
191
191
  User.attr_encodable :permissions => :deez_permissions
192
192
  @user.as_json.should == {'deez_permissions' => @user.permissions.as_json}
193
193
  end
194
-
194
+
195
195
  it "should let me specify a prefix to a set of attr_encodable's" do
196
196
  User.attr_encodable :id, :first_name, :foobar, :permissions, :prefix => :t
197
197
  @user.as_json.should == {'t_id' => @user.id, 't_first_name' => @user.first_name, 't_foobar' => 'baz', 't_permissions' => @user.permissions.as_json}
198
198
  end
199
199
  end
200
-
200
+
201
201
  it "should propagate down subclasses as well" do
202
202
  User.attr_encodable :name
203
203
  class SubUser < User; end
204
204
  SubUser.unencodable_attributes.should == User.unencodable_attributes
205
205
  end
206
-
206
+
207
207
  describe "named groups" do
208
208
  it "should be supported on a class-basis with a :name option" do
209
209
  User.attr_unencodable :id
@@ -211,13 +211,13 @@ describe Encodable do
211
211
  User.attr_encodable :id, :first_name, :last_name, :as => :short
212
212
  User.all.as_json(:short).should == [{'id' => 1, 'first_name' => 'flip', 'last_name' => 'sasser'}]
213
213
  end
214
-
214
+
215
215
  it "should be supported on an instance-basis with a :name option" do
216
216
  User.attr_encodable :id, :first_name, :last_name, :as => :short
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
-
220
+
221
221
  it "should also create a named_scope that limits the SELECT statement to the included attributes" do
222
222
  User.attr_encodable :id, :as => :short
223
223
  User.first.first_name.should == 'flip'
@@ -225,4 +225,35 @@ describe Encodable do
225
225
  User.short.first.id.should == 1
226
226
  end
227
227
  end
228
+
229
+ describe "additive changes" do
230
+ it "should allow me to add columns after defining a default group" do
231
+ User.attr_encodable :id
232
+ @user.as_json.should == {'id' => 1}
233
+ User.attr_encodable :first_name
234
+ @user.as_json.should == {'id' => 1, 'first_name' => 'flip'}
235
+ end
236
+
237
+ it "should allow me to add columns after defining an alias" do
238
+ User.attr_encodable :id => :slug
239
+ @user.as_json.should == {'slug' => 1}
240
+ User.attr_encodable :first_name
241
+ @user.as_json.should == {'slug' => 1, 'first_name' => 'flip'}
242
+ end
243
+
244
+ it "should allow me to add columns after defining a group named 'other'" do
245
+ User.attr_encodable :id, :as => :other
246
+ @user.as_json(:other).should == {'id' => 1}
247
+ User.attr_encodable :first_name, :as => :other
248
+ @user.as_json(:other).should == {'id' => 1, 'first_name' => 'flip'}
249
+ end
250
+
251
+ it "should allow me to add columns after defining an alias on a group named 'other'" do
252
+ User.attr_encodable({:id => :slug}, :as => :other)
253
+ User.default_attributes(:other).should == [:id]
254
+ @user.as_json(:other).should == {'slug' => 1}
255
+ User.attr_encodable :first_name, :as => :other
256
+ @user.as_json(:other).should == {'slug' => 1, 'first_name' => 'flip'}
257
+ end
258
+ end
228
259
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_encodable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec