attr_encodable 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/attr_encodable.rb +34 -29
- data/spec/attr_encodable_spec.rb +19 -7
- metadata +36 -68
data/lib/attr_encodable.rb
CHANGED
@@ -80,33 +80,36 @@ module Encodable
|
|
80
80
|
|
81
81
|
module InstanceMethods
|
82
82
|
def serializable_hash(options = {})
|
83
|
+
options ||= {}
|
84
|
+
original_except = if options[:except]
|
85
|
+
options[:except] = Array(options[:except]).map(&:to_sym)
|
86
|
+
else
|
87
|
+
options[:except] = []
|
88
|
+
end
|
89
|
+
|
90
|
+
# Convert :only to :except
|
83
91
|
if options && options[:only]
|
84
|
-
|
85
|
-
|
92
|
+
options[:except].push *self.class.default_attributes - Array(options.delete(:only).map(&:to_sym))
|
93
|
+
end
|
94
|
+
|
95
|
+
# This is a little bit confusing. ActiveRecord's default behavior is to apply the :except arguments you pass
|
96
|
+
# in to any :include options UNLESS it's overridden on the :include option. In the event that we have some
|
97
|
+
# *default* excepts that come from Encodable, we want to ignore those and pass only whatever the original
|
98
|
+
# :except options from the user were on down to the :include guys.
|
99
|
+
inherited_except = original_except - self.class.default_attributes
|
100
|
+
case options[:include]
|
101
|
+
when Array, Symbol
|
102
|
+
# Convert includes arrays or singleton symbols into a hash with our original_except scope
|
103
|
+
includes = Array(options[:include])
|
104
|
+
options[:include] = Hash[*includes.map{|association| [association, {:except => inherited_except}]}.flatten]
|
86
105
|
else
|
87
|
-
options ||= {}
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
# in to any :include options UNLESS it's overridden on the :include option. In the event that we have some
|
95
|
-
# *default* excepts that come from Encodable, we want to ignore those and pass only whatever the original
|
96
|
-
# :except options from the user were on down to the :include guys.
|
97
|
-
inherited_except = original_except - self.class.default_attributes
|
98
|
-
case options[:include]
|
99
|
-
when Array, Symbol
|
100
|
-
# Convert includes arrays or singleton symbols into a hash with our original_except scope
|
101
|
-
includes = Array(options[:include])
|
102
|
-
options[:include] = Hash[*includes.map{|association| [association, {:except => inherited_except}]}.flatten]
|
103
|
-
else
|
104
|
-
options[:include] ||= {}
|
105
|
-
end
|
106
|
-
# Exclude the black-list
|
107
|
-
options[:except].push *self.class.unencodable_attributes
|
108
|
-
# Include any default :include or :methods arguments that were passed in earlier
|
109
|
-
self.class.default_attributes.each do |attribute, as|
|
106
|
+
options[:include] ||= {}
|
107
|
+
end
|
108
|
+
# Exclude the black-list
|
109
|
+
options[:except].push *self.class.unencodable_attributes
|
110
|
+
# Include any default :include or :methods arguments that were passed in earlier
|
111
|
+
self.class.default_attributes.each do |attribute, as|
|
112
|
+
unless options[:except].include?(attribute)
|
110
113
|
if association = self.class.reflect_on_association(attribute)
|
111
114
|
options[:include][attribute] = {:except => inherited_except}
|
112
115
|
elsif respond_to?(attribute) && !self.class.column_names.include?(attribute.to_s)
|
@@ -114,14 +117,16 @@ module Encodable
|
|
114
117
|
options[:methods].push attribute
|
115
118
|
end
|
116
119
|
end
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
+
end
|
121
|
+
as_json = super(options)
|
122
|
+
unless self.class.renamed_encoded_attributes.empty?
|
123
|
+
self.class.renamed_encoded_attributes.each do |attribute, as|
|
124
|
+
if as_json.has_key?(attribute) || as_json.has_key?(attribute.to_s)
|
120
125
|
as_json[as.to_s] = as_json.delete(attribute) || as_json.delete(attribute.to_s)
|
121
126
|
end
|
122
127
|
end
|
123
|
-
as_json
|
124
128
|
end
|
129
|
+
as_json
|
125
130
|
end
|
126
131
|
end
|
127
132
|
end
|
data/spec/attr_encodable_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require "lib/attr_encodable"
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'attr_encodable')#{}"../lib/attr_encodable"
|
2
|
+
require 'active_support'
|
2
3
|
|
3
4
|
describe Encodable do
|
4
5
|
it "should automatically extend ActiveRecord::Base" do
|
@@ -38,7 +39,7 @@ describe Encodable do
|
|
38
39
|
:first_name => "flip",
|
39
40
|
:last_name => "sasser",
|
40
41
|
:email => "flip@foobar.com",
|
41
|
-
:encrypted_password =>
|
42
|
+
:encrypted_password => SecureRandom.hex(30),
|
42
43
|
:developer => true,
|
43
44
|
:admin => true,
|
44
45
|
:password_set => true,
|
@@ -69,15 +70,15 @@ describe Encodable do
|
|
69
70
|
|
70
71
|
describe "at the parent model level" do
|
71
72
|
it "should not mess with to_json unless when attr_encodable and attr_unencodable are not set" do
|
72
|
-
@user.as_json == @user.attributes
|
73
|
+
@user.as_json.should == @user.attributes
|
73
74
|
end
|
74
75
|
|
75
76
|
it "should not mess with :include options" do
|
76
|
-
@user.as_json(:include => :permissions) == @user.attributes.merge(:permissions => @user.permissions.as_json)
|
77
|
+
@user.as_json(:include => :permissions).should == @user.attributes.merge(:permissions => @user.permissions.as_json)
|
77
78
|
end
|
78
79
|
|
79
80
|
it "should not mess with :methods options" do
|
80
|
-
@user.as_json(:methods => :foobar) == @user.attributes.merge(:foobar => "baz")
|
81
|
+
@user.as_json(:methods => :foobar).should == @user.attributes.merge(:foobar => "baz")
|
81
82
|
end
|
82
83
|
|
83
84
|
it "should allow me to whitelist attributes" do
|
@@ -90,6 +91,7 @@ describe Encodable do
|
|
90
91
|
@user.as_json.should == @user.attributes.except('login', 'first_name', 'last_name')
|
91
92
|
end
|
92
93
|
|
94
|
+
|
93
95
|
# Of note is the INSANITY of ActiveRecord in that it applies :only / :except to :include as well. Which is
|
94
96
|
# obviously insane. Similarly, it doesn't allow :methods to come along when :only is specified. Good god, what
|
95
97
|
# a shame.
|
@@ -109,13 +111,13 @@ describe Encodable do
|
|
109
111
|
end
|
110
112
|
end
|
111
113
|
|
112
|
-
describe "at the child model level when the
|
114
|
+
describe "at the child model level when the parent model has attr_encodable set" do
|
113
115
|
before :each do
|
114
116
|
User.attr_encodable :login, :first_name, :last_name
|
115
117
|
end
|
116
118
|
|
117
119
|
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
|
118
|
-
@user.permissions.as_json == @user.permissions.map(&:attributes)
|
120
|
+
@user.permissions.as_json.should == @user.permissions.map(&:attributes)
|
119
121
|
end
|
120
122
|
|
121
123
|
it "should not mess with :include options" do
|
@@ -153,6 +155,16 @@ describe Encodable do
|
|
153
155
|
@user.as_json.should == @user.attributes.slice('login', 'first_name', 'id').merge(:foobar => "baz")
|
154
156
|
end
|
155
157
|
|
158
|
+
it "should allow me to only request certain whitelisted attributes and methods" do
|
159
|
+
User.attr_encodable :login, :first_name, :last_name, :foobar
|
160
|
+
@user.as_json(:only => [:login, :foobar]).should == {'login' => 'flipsasser', :foobar => 'baz'}
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should allow me to use :only with aliased methods and attributes" do
|
164
|
+
User.attr_encodable :login => :login_eh, :first_name => :foist, :last_name => :last, :foobar => :baz
|
165
|
+
@user.as_json(:only => [:login, :foobar]).should == {'login_eh' => 'flipsasser', 'baz' => 'baz'}
|
166
|
+
end
|
167
|
+
|
156
168
|
describe "reassigning" do
|
157
169
|
it "should let me reassign attributes" do
|
158
170
|
User.attr_encodable :id => :identifier
|
metadata
CHANGED
@@ -1,101 +1,69 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_encodable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 6
|
10
|
-
version: 0.0.6
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Flip Sasser
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
name: rcov
|
23
|
-
prerelease: false
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
12
|
+
date: 2012-10-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
- 9
|
33
|
-
- 9
|
34
|
-
version: 0.9.9
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
35
22
|
type: :development
|
36
|
-
version_requirements: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: rspec
|
39
23
|
prerelease: false
|
40
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
25
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
version: "2.0"
|
50
|
-
type: :development
|
51
|
-
version_requirements: *id002
|
52
|
-
description: "\n attr_encodable enables you to set up defaults for what is included or excluded when you serialize an ActiveRecord object. This is especially useful for protecting private attributes when building a public API.\n "
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
description: ! "\n attr_encodable enables you to set up defaults for what is included
|
31
|
+
or excluded when you serialize an ActiveRecord object. This is especially useful\n
|
32
|
+
\ for protecting private attributes when building a public API.\n "
|
53
33
|
email: flip@x451.com
|
54
34
|
executables: []
|
55
|
-
|
56
35
|
extensions: []
|
57
|
-
|
58
|
-
extra_rdoc_files:
|
36
|
+
extra_rdoc_files:
|
59
37
|
- LICENSE
|
60
38
|
- README.md
|
61
|
-
files:
|
39
|
+
files:
|
62
40
|
- LICENSE
|
63
41
|
- README.md
|
64
42
|
- lib/attr_encodable.rb
|
65
43
|
- spec/attr_encodable_spec.rb
|
66
|
-
has_rdoc: true
|
67
44
|
homepage: http://github.com/Plinq/attr_encodable
|
68
45
|
licenses: []
|
69
|
-
|
70
46
|
post_install_message:
|
71
47
|
rdoc_options: []
|
72
|
-
|
73
|
-
require_paths:
|
48
|
+
require_paths:
|
74
49
|
- lib
|
75
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
51
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
|
82
|
-
- 0
|
83
|
-
version: "0"
|
84
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
57
|
none: false
|
86
|
-
requirements:
|
87
|
-
- -
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
segments:
|
91
|
-
- 0
|
92
|
-
version: "0"
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
93
62
|
requirements: []
|
94
|
-
|
95
63
|
rubyforge_project:
|
96
|
-
rubygems_version: 1.
|
64
|
+
rubygems_version: 1.8.24
|
97
65
|
signing_key:
|
98
66
|
specification_version: 3
|
99
67
|
summary: An attribute black- or white-list for ActiveRecord serialization
|
100
|
-
test_files:
|
68
|
+
test_files:
|
101
69
|
- spec/attr_encodable_spec.rb
|