unveil 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/unveil/core_ext.rb +2 -2
- data/lib/unveil/error.rb +4 -0
- data/lib/unveil.rb +17 -7
- data/test/fixtures/users.yml +9 -0
- data/test/schema.rb +7 -0
- data/test/test_helper.rb +18 -0
- data/test/unveil_test.rb +50 -3
- metadata +6 -3
data/lib/unveil/core_ext.rb
CHANGED
data/lib/unveil/error.rb
ADDED
data/lib/unveil.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'unveil/error'
|
1
2
|
require 'unveil/core_ext'
|
2
3
|
|
3
4
|
module Unveil
|
@@ -6,17 +7,26 @@ module Unveil
|
|
6
7
|
end
|
7
8
|
|
8
9
|
module ClassMethods
|
9
|
-
def unveil(
|
10
|
-
|
11
|
-
self.
|
12
|
-
|
10
|
+
def unveil(*args)
|
11
|
+
name = args[0].is_a?(Symbol) ? args.shift : :default
|
12
|
+
self.unveil_scopes[name] = args[0] || {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def unveil_scopes
|
16
|
+
@@unveil_scopes ||= {:default => {}}
|
13
17
|
end
|
14
18
|
end
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
|
20
|
+
def unveil(*args)
|
21
|
+
name = args[0].is_a?(Symbol) ? args.shift : :default
|
22
|
+
options = args[0] || {}
|
23
|
+
|
24
|
+
unless self.class.unveil_scopes[name]
|
25
|
+
raise Unveil::UndefinedScopeError,
|
26
|
+
":#{name} hasn't been defined. Try adding `unveil :#{name}` to #{self.class.name}."
|
19
27
|
end
|
28
|
+
|
29
|
+
return serializable_hash(self.class.unveil_scopes[name].merge(options))
|
20
30
|
end
|
21
31
|
end
|
22
32
|
|
data/test/schema.rb
ADDED
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,21 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
|
+
require 'active_record'
|
4
|
+
require 'active_record/fixtures'
|
3
5
|
require 'active_support'
|
6
|
+
|
7
|
+
def load_schema
|
8
|
+
# Connect to the database
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
10
|
+
|
11
|
+
# Load the schema
|
12
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
13
|
+
|
14
|
+
# Create the fixtures
|
15
|
+
fixtures = ActiveRecord::Schema.tables
|
16
|
+
fixtures.delete('schema_migrations')
|
17
|
+
Fixtures.create_fixtures(File.dirname(__FILE__) + '/fixtures', fixtures)
|
18
|
+
|
19
|
+
# Require Unveil
|
20
|
+
require File.dirname(__FILE__) + '/../init'
|
21
|
+
end
|
data/test/unveil_test.rb
CHANGED
@@ -1,8 +1,55 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class UnveilTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
load_schema
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
unveil :limited, :except => :email
|
8
|
+
end
|
9
|
+
|
10
|
+
test 'should define unveil in objects of ActiveRecord::Base' do
|
11
|
+
assert User.first.unveil
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'should use the default scope when no scope is specified' do
|
15
|
+
default_user = { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' }
|
16
|
+
|
17
|
+
user = User.first
|
18
|
+
|
19
|
+
assert_equal user.unveil(:default), user.unveil
|
20
|
+
assert_equal user.unveil, default_user
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'should use a named scope when specified' do
|
24
|
+
limited_user = { 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith' }
|
25
|
+
|
26
|
+
user = User.first
|
27
|
+
|
28
|
+
assert_not_equal user.unveil, user.unveil(:limited)
|
29
|
+
assert_equal user.unveil(:limited), limited_user
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'should call unveil for each object in an array' do
|
33
|
+
users = [
|
34
|
+
{ 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' },
|
35
|
+
{ 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith', 'email' => 'janesmith@example.com' }
|
36
|
+
]
|
37
|
+
|
38
|
+
assert_equal User.all.unveil, users
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'should use a named scope for each object in an array when specified' do
|
42
|
+
default_users = [
|
43
|
+
{ 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith', 'email' => 'johnsmith@example.com' },
|
44
|
+
{ 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith', 'email' => 'janesmith@example.com' }
|
45
|
+
]
|
46
|
+
|
47
|
+
limited_users = [
|
48
|
+
{ 'id' => 1, 'first_name' => 'John', 'last_name' => 'Smith' },
|
49
|
+
{ 'id' => 2, 'first_name' => 'Jane', 'last_name' => 'Smith' }
|
50
|
+
]
|
51
|
+
|
52
|
+
assert_equal User.all.unveil, default_users
|
53
|
+
assert_equal User.all.unveil(:limited), limited_users
|
7
54
|
end
|
8
55
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Shaun Chapman
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-02-15 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -29,7 +29,10 @@ extra_rdoc_files: []
|
|
29
29
|
files:
|
30
30
|
- Rakefile
|
31
31
|
- lib/unveil/core_ext.rb
|
32
|
+
- lib/unveil/error.rb
|
32
33
|
- lib/unveil.rb
|
34
|
+
- test/fixtures/users.yml
|
35
|
+
- test/schema.rb
|
33
36
|
- test/test_helper.rb
|
34
37
|
- test/unveil_test.rb
|
35
38
|
has_rdoc: true
|