resourced 0.0.1.beta1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/Gemfile +13 -2
- data/README.md +26 -5
- data/lib/generators/rails/resourced_generator.rb +32 -0
- data/lib/generators/rails/templates/resource.rb +17 -0
- data/lib/generators/resource_override.rb +13 -0
- data/lib/generators/rspec/resourced_generator.rb +9 -0
- data/lib/generators/rspec/templates/resource_spec.rb +4 -0
- data/lib/generators/test_unit/resourced_generator.rb +9 -0
- data/lib/generators/test_unit/templates/resource_test.rb +4 -0
- data/lib/resourced.rb +8 -8
- data/lib/resourced/active_record.rb +1 -3
- data/lib/resourced/active_record/actions.rb +7 -7
- data/lib/resourced/{params.rb → attributes.rb} +49 -22
- data/lib/resourced/finders.rb +9 -7
- data/lib/resourced/railtie.rb +21 -0
- data/lib/resourced/version.rb +1 -1
- data/resourced.gemspec +1 -1
- data/spec/resourced/adapters/active_record_spec.rb +1 -1
- data/spec/resourced/{params_spec.rb → attributes_spec.rb} +46 -19
- data/spec/resourced/finders_spec.rb +13 -0
- data/spec/resourced/resource_spec.rb +104 -0
- metadata +19 -10
- data/lib/resourced/active_record/meta.rb +0 -15
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -5,6 +5,17 @@ gemspec
|
|
5
5
|
|
6
6
|
group :test do
|
7
7
|
gem "rake"
|
8
|
-
gem "
|
9
|
-
|
8
|
+
gem "activerecord", "~> 3.2"
|
9
|
+
end
|
10
|
+
|
11
|
+
platforms :ruby do
|
12
|
+
group :test do
|
13
|
+
gem "sqlite3"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
platforms :jruby do
|
18
|
+
group :test do
|
19
|
+
gem "activerecord-jdbcsqlite3-adapter"
|
20
|
+
end
|
10
21
|
end
|
data/README.md
CHANGED
@@ -10,11 +10,13 @@ class PostResource
|
|
10
10
|
include Resourced::ActiveRecord
|
11
11
|
|
12
12
|
model Post
|
13
|
-
|
13
|
+
body :post
|
14
|
+
key :id
|
14
15
|
|
15
|
-
|
16
|
-
allow :title, :body
|
17
|
-
allow :
|
16
|
+
attributes do
|
17
|
+
allow :title, :body
|
18
|
+
allow :tags, as: :array
|
19
|
+
allow :category, as: :string, if: -> { scope.admin? }
|
18
20
|
end
|
19
21
|
|
20
22
|
finders do
|
@@ -27,6 +29,10 @@ class PostResource
|
|
27
29
|
finder :search do |val|
|
28
30
|
chain.where(t[:body].matches("%#{val}%"))
|
29
31
|
end
|
32
|
+
|
33
|
+
finder :limit, as: :integer, default: 20 do |val|
|
34
|
+
chain.limit(20)
|
35
|
+
end
|
30
36
|
end
|
31
37
|
end
|
32
38
|
```
|
@@ -54,7 +60,7 @@ posts.all # Will return all posts which contains "Atlas Shrugged" unless current
|
|
54
60
|
|
55
61
|
Add this line to your application's Gemfile:
|
56
62
|
|
57
|
-
gem 'resourced'
|
63
|
+
gem 'resourced', '~> 0.1'
|
58
64
|
|
59
65
|
And then execute:
|
60
66
|
|
@@ -66,6 +72,21 @@ Or install it yourself as:
|
|
66
72
|
|
67
73
|
## Usage
|
68
74
|
|
75
|
+
### Creating Resources
|
76
|
+
|
77
|
+
If you are using rails, your resource files will be automatically generated with models or resources.
|
78
|
+
|
79
|
+
```
|
80
|
+
rails generate resource Something field1:string field2:integer
|
81
|
+
```
|
82
|
+
|
83
|
+
You can also generate resource file manually:
|
84
|
+
```
|
85
|
+
rails generate resourced Something field1:string field2:integer
|
86
|
+
```
|
87
|
+
|
88
|
+
This will create folder `app/resources` if not exists, and basic resource file.
|
89
|
+
|
69
90
|
## Contributing
|
70
91
|
|
71
92
|
1. Fork it
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
|
6
|
+
class ResourcedGenerator < Rails::Generators::NamedBase
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
check_class_collision :suffix => "Resource"
|
9
|
+
|
10
|
+
argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
|
11
|
+
|
12
|
+
def create_resourced_file
|
13
|
+
template "resource.rb", File.join("app/resources", class_path, "#{file_name}_resource.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
hook_for :test_framework
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def accessible_attributes
|
21
|
+
attributes.reject(&:reference?)
|
22
|
+
end
|
23
|
+
|
24
|
+
def type_for(attr)
|
25
|
+
type = attr.type
|
26
|
+
type = :string if type == :text
|
27
|
+
type
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class <%= class_name %>Resource
|
2
|
+
include Resourced::ActiveRecord
|
3
|
+
|
4
|
+
model <%= class_name %>
|
5
|
+
body :<%= singular_name %>
|
6
|
+
key :id
|
7
|
+
|
8
|
+
attributes do
|
9
|
+
<% accessible_attributes.each do |attr| -%>
|
10
|
+
allow :<%= attr.name %>, as: :<%= type_for(attr) %>
|
11
|
+
<% end -%>
|
12
|
+
end
|
13
|
+
|
14
|
+
finders do
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Taken from draper gem https://github.com/drapergem/draper/
|
2
|
+
require "rails/generators"
|
3
|
+
require "rails/generators/rails/resource/resource_generator"
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
module Generators
|
7
|
+
ResourceGenerator.class_eval do
|
8
|
+
def add_resource
|
9
|
+
invoke "resourced"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Rspec
|
2
|
+
class ResourcedGenerator < ::Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def create_spec_file
|
6
|
+
template 'resource_spec.rb', File.join('spec/resources', class_path, "#{file_name}_resource_spec.rb")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module TestUnit
|
2
|
+
class ResourcedGenerator < ::Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path('../templates', __FILE__)
|
4
|
+
|
5
|
+
def create_test_file
|
6
|
+
template 'resource_test.rb', File.join('test/resources', class_path, "#{file_name}_resource_test.rb")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
data/lib/resourced.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
require "resourced/version"
|
2
|
-
require "resourced/
|
2
|
+
require "resourced/attributes"
|
3
3
|
require "resourced/finders"
|
4
|
+
require "resourced/railtie" if defined?(Rails)
|
4
5
|
|
5
6
|
module Resourced
|
6
|
-
module
|
7
|
+
module Resource
|
7
8
|
module InstanceMethods
|
8
|
-
def initialize(params, scope)
|
9
|
+
def initialize(params, scope=nil)
|
9
10
|
@scope = scope
|
10
11
|
@model = self.class.instance_variable_get(:@model)
|
11
12
|
@key = self.class.instance_variable_get(:@key)
|
12
13
|
@chain = @model
|
13
14
|
super
|
14
|
-
@body = @params.keep_if { |k, v| attribute_names.include?(k) }
|
15
15
|
end
|
16
|
-
attr_accessor :
|
16
|
+
attr_accessor :scope
|
17
17
|
attr_reader :model, :chain, :key
|
18
18
|
|
19
19
|
##
|
20
|
-
# Run external code in context of
|
20
|
+
# Run external code in context of resource
|
21
21
|
#
|
22
22
|
# Examples:
|
23
23
|
#
|
@@ -45,7 +45,7 @@ module Resourced
|
|
45
45
|
end
|
46
46
|
|
47
47
|
##
|
48
|
-
# Duplicate
|
48
|
+
# Duplicate resource and set another model class
|
49
49
|
#
|
50
50
|
def [](model_class)
|
51
51
|
klass = self.dup
|
@@ -62,7 +62,7 @@ module Resourced
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def self.included(base)
|
65
|
-
base.send(:include, Resourced::
|
65
|
+
base.send(:include, Resourced::Attributes)
|
66
66
|
base.send(:include, Resourced::Finders)
|
67
67
|
base.send(:include, InstanceMethods)
|
68
68
|
base.extend ClassMethods
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require "resourced"
|
2
|
-
require "resourced/active_record/meta"
|
3
2
|
require "resourced/active_record/proxy"
|
4
3
|
require "resourced/active_record/actions"
|
5
4
|
|
@@ -25,8 +24,7 @@ module Resourced
|
|
25
24
|
end
|
26
25
|
|
27
26
|
def self.included(base)
|
28
|
-
base.send(:include, Resourced::
|
29
|
-
base.send(:include, Meta)
|
27
|
+
base.send(:include, Resourced::Resource)
|
30
28
|
base.send(:include, Proxy)
|
31
29
|
base.send(:include, Actions)
|
32
30
|
base.extend ClassMethods
|
@@ -3,14 +3,14 @@ module Resourced
|
|
3
3
|
|
4
4
|
module Actions
|
5
5
|
def build
|
6
|
-
model.new(@
|
6
|
+
model.new(@attributes)
|
7
7
|
end
|
8
8
|
|
9
9
|
def update
|
10
|
-
body = @
|
10
|
+
body = @attributes.reject { |k,_| k == :id }
|
11
11
|
|
12
|
-
collection = if
|
13
|
-
[model.find(
|
12
|
+
collection = if attributes[key]
|
13
|
+
[model.find(attributes[key])]
|
14
14
|
else
|
15
15
|
all
|
16
16
|
end
|
@@ -22,10 +22,10 @@ module Resourced
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def update!
|
25
|
-
body = @
|
25
|
+
body = @attributes.reject { |k,_| k == :id }
|
26
26
|
|
27
|
-
collection = if
|
28
|
-
[model.find(
|
27
|
+
collection = if @attributes[key]
|
28
|
+
[model.find(@attributes[key])]
|
29
29
|
else
|
30
30
|
all
|
31
31
|
end
|
@@ -1,12 +1,14 @@
|
|
1
|
-
require "
|
1
|
+
require "coercible"
|
2
2
|
|
3
3
|
module Resourced
|
4
|
-
module
|
4
|
+
module Attributes
|
5
5
|
|
6
6
|
module InstanceMethods
|
7
7
|
def initialize(params, scope)
|
8
|
-
|
8
|
+
@attr_body = self.class.body
|
9
|
+
set(@attr_body ? params[@attr_body] : params)
|
9
10
|
end
|
11
|
+
attr_reader :attributes, :attr_body
|
10
12
|
|
11
13
|
##
|
12
14
|
# Set additional params
|
@@ -20,12 +22,12 @@ module Resourced
|
|
20
22
|
# resource.set(role: "guest")
|
21
23
|
#
|
22
24
|
def set(params={})
|
23
|
-
sanitized = self.class.
|
25
|
+
sanitized = self.class._attributes_obj.sanitize_params(self, params)
|
24
26
|
|
25
|
-
if @
|
26
|
-
@
|
27
|
+
if @attributes
|
28
|
+
@attributes.merge(sanitized)
|
27
29
|
else
|
28
|
-
@
|
30
|
+
@attributes = sanitized
|
29
31
|
end
|
30
32
|
|
31
33
|
self
|
@@ -44,21 +46,16 @@ module Resourced
|
|
44
46
|
#
|
45
47
|
def erase(*keys)
|
46
48
|
keys.each do |key|
|
47
|
-
@
|
49
|
+
@attributes.delete(key.to_sym)
|
48
50
|
end
|
49
51
|
|
50
52
|
self
|
51
53
|
end
|
52
|
-
|
53
|
-
protected
|
54
|
-
|
55
|
-
def params
|
56
|
-
@params
|
57
|
-
end
|
58
54
|
end
|
59
55
|
|
60
56
|
class RuleSet
|
61
57
|
def initialize
|
58
|
+
@types = {}
|
62
59
|
@defaults = {}
|
63
60
|
@conditional_allows = []
|
64
61
|
@unconditional_allows = []
|
@@ -80,7 +77,7 @@ module Resourced
|
|
80
77
|
# allow :role, default: "guest"
|
81
78
|
#
|
82
79
|
def allow(*fields)
|
83
|
-
opts = fields
|
80
|
+
opts = extract_options(fields)
|
84
81
|
|
85
82
|
if opts[:if]
|
86
83
|
@conditional_allows << ConditionalGroup.new(opts[:if], fields)
|
@@ -88,6 +85,12 @@ module Resourced
|
|
88
85
|
@unconditional_allows += fields
|
89
86
|
end
|
90
87
|
|
88
|
+
if as = opts[:as]
|
89
|
+
fields.each do |field|
|
90
|
+
@types[field] = :"to_#{as}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
91
94
|
if opts[:default]
|
92
95
|
fields.each do |field|
|
93
96
|
@defaults[field] = opts[:default]
|
@@ -108,7 +111,7 @@ module Resourced
|
|
108
111
|
# restrict :role, if: -> { scope !== :admin }
|
109
112
|
#
|
110
113
|
def restrict(*fields)
|
111
|
-
opts = fields
|
114
|
+
opts = extract_options(fields)
|
112
115
|
|
113
116
|
if opts[:if]
|
114
117
|
@conditional_restricts << ConditionalGroup.new(opts[:if], fields)
|
@@ -138,7 +141,27 @@ module Resourced
|
|
138
141
|
end
|
139
142
|
end
|
140
143
|
|
141
|
-
|
144
|
+
result = {}
|
145
|
+
coercer = Coercible::Coercer.new unless @types.empty?
|
146
|
+
|
147
|
+
@defaults.merge(params).each do |k, v|
|
148
|
+
k = k.to_sym
|
149
|
+
|
150
|
+
if allowed_params.include?(k)
|
151
|
+
result[k] = @types[k] ? coercer[v.class].public_send(@types[k], v) : v
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
result
|
156
|
+
end
|
157
|
+
|
158
|
+
private
|
159
|
+
def extract_options(args)
|
160
|
+
if args.last.is_a?(Hash)
|
161
|
+
args.pop
|
162
|
+
else
|
163
|
+
{}
|
164
|
+
end
|
142
165
|
end
|
143
166
|
end
|
144
167
|
|
@@ -162,18 +185,22 @@ module Resourced
|
|
162
185
|
end
|
163
186
|
|
164
187
|
module ClassMethods
|
165
|
-
def
|
166
|
-
@
|
167
|
-
@
|
188
|
+
def attributes(&block)
|
189
|
+
@_attributes_obj ||= RuleSet.new
|
190
|
+
@_attributes_obj.instance_eval(&block)
|
191
|
+
end
|
192
|
+
|
193
|
+
def body(name=nil)
|
194
|
+
name ? @attr_body = name : @attr_body
|
168
195
|
end
|
169
196
|
|
170
|
-
attr_reader :
|
197
|
+
attr_reader :_attributes_obj
|
171
198
|
end
|
172
199
|
|
173
200
|
include InstanceMethods
|
174
201
|
|
175
202
|
def self.included(base)
|
176
|
-
base.extend
|
203
|
+
base.extend ClassMethods
|
177
204
|
end
|
178
205
|
end
|
179
206
|
end
|
data/lib/resourced/finders.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require "resourced/
|
1
|
+
require "resourced/attributes"
|
2
2
|
|
3
3
|
module Resourced
|
4
4
|
module Finders
|
5
|
-
class Finders < Resourced::
|
5
|
+
class Finders < Resourced::Attributes::RuleSet
|
6
6
|
def initialize
|
7
7
|
super
|
8
8
|
@finders = {}
|
@@ -40,15 +40,16 @@ module Resourced
|
|
40
40
|
def initialize(params, scope)
|
41
41
|
super
|
42
42
|
@finders_obj = self.class.instance_variable_get(:@_finders_obj)
|
43
|
-
@finders = @finders_obj.sanitize_params(
|
43
|
+
@finders = @finders_obj.sanitize_params(self, params)
|
44
|
+
end
|
45
|
+
attr_reader :finders
|
44
46
|
|
47
|
+
def apply_finders
|
45
48
|
defaults = self.class.instance_variable_get(:@_default_finders)
|
46
49
|
defaults.each do |finder|
|
47
|
-
|
50
|
+
@chain = self.instance_eval(&finder)
|
48
51
|
end
|
49
|
-
end
|
50
52
|
|
51
|
-
def apply_finders
|
52
53
|
@finders.each_pair do |key, value|
|
53
54
|
@chain = self.instance_exec(value, &@finders_obj.finders[key.to_sym])
|
54
55
|
end
|
@@ -60,11 +61,12 @@ module Resourced
|
|
60
61
|
module ClassMethods
|
61
62
|
def finders(&block)
|
62
63
|
@_finders_obj ||= Finders.new
|
63
|
-
@_finders_obj.instance_eval(&block)
|
64
|
+
@_finders_obj.instance_eval(&block) if block_given?
|
64
65
|
end
|
65
66
|
attr_reader :_finders_obj
|
66
67
|
|
67
68
|
def default_finder(&block)
|
69
|
+
finders
|
68
70
|
@_default_finders << block if block_given?
|
69
71
|
end
|
70
72
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "rails/railtie"
|
2
|
+
require "resourced/active_record"
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
generators do |app|
|
7
|
+
app ||= Rails.application # Rails 3.0.x does not yield `app`
|
8
|
+
|
9
|
+
Rails::Generators.configure! app.config.generators
|
10
|
+
require "generators/resource_override"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Resourced
|
16
|
+
class Railtie < Rails::Railtie
|
17
|
+
config.after_initialize do |app|
|
18
|
+
app.config.paths.add "app/resources", eager_load: true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/resourced/version.rb
CHANGED
data/resourced.gemspec
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Resourced::
|
4
|
-
class
|
5
|
-
include Resourced::
|
3
|
+
describe Resourced::Attributes do
|
4
|
+
class AttributesTest
|
5
|
+
include Resourced::Attributes
|
6
6
|
|
7
|
-
def initialize(params, scope)
|
7
|
+
def initialize(params, scope=nil)
|
8
8
|
@scope = scope
|
9
9
|
super
|
10
10
|
end
|
11
|
-
attr_reader :
|
11
|
+
attr_reader :attributes
|
12
12
|
end
|
13
13
|
|
14
14
|
params = { :a => 1, :b => 2, :c => 3, :d => 4 }
|
15
15
|
|
16
16
|
describe "Unconditional allows" do
|
17
|
-
klass =
|
18
|
-
klass.
|
17
|
+
klass = AttributesTest.dup
|
18
|
+
klass.attributes do
|
19
19
|
allow :a, :b, :c
|
20
20
|
end
|
21
21
|
inst = klass.new(params, "admin")
|
22
22
|
|
23
23
|
it "should contain only allowed" do
|
24
|
-
inst.
|
24
|
+
inst.attributes.keys.should eq([:a, :b, :c])
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
describe "Conditional allows" do
|
29
|
-
klass =
|
29
|
+
klass = AttributesTest.dup
|
30
30
|
|
31
|
-
klass.
|
31
|
+
klass.attributes do
|
32
32
|
allow :a, :b, :c
|
33
33
|
allow :d, :if => lambda { @scope == "admin" }
|
34
34
|
end
|
@@ -37,7 +37,7 @@ describe Resourced::Params do
|
|
37
37
|
inst = klass.new(params, "admin")
|
38
38
|
|
39
39
|
it "should contain conditional param" do
|
40
|
-
inst.
|
40
|
+
inst.attributes.keys.should eq([:a, :b, :c, :d])
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -45,27 +45,27 @@ describe Resourced::Params do
|
|
45
45
|
inst = klass.new(params, "guest")
|
46
46
|
|
47
47
|
it "should not contain conditional param" do
|
48
|
-
inst.
|
48
|
+
inst.attributes.keys.should eq([:a, :b, :c])
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
53
|
describe "Unconditional restricts" do
|
54
|
-
klass =
|
55
|
-
klass.
|
54
|
+
klass = AttributesTest.dup
|
55
|
+
klass.attributes do
|
56
56
|
allow :a, :b, :c
|
57
57
|
restrict :c
|
58
58
|
end
|
59
59
|
inst = klass.new(params, "admin")
|
60
60
|
|
61
61
|
it "should not contain restricted" do
|
62
|
-
inst.
|
62
|
+
inst.attributes.keys.should eq([:a, :b])
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
66
|
describe "Conditional restricts" do
|
67
|
-
klass =
|
68
|
-
klass.
|
67
|
+
klass = AttributesTest.dup
|
68
|
+
klass.attributes do
|
69
69
|
allow :a, :b, :c
|
70
70
|
restrict :c, :if => lambda { @scope != "admin" }
|
71
71
|
end
|
@@ -74,7 +74,7 @@ describe Resourced::Params do
|
|
74
74
|
inst = klass.new(params, "guest")
|
75
75
|
|
76
76
|
it "should not contain conditional param" do
|
77
|
-
inst.
|
77
|
+
inst.attributes.keys.should eq([:a, :b])
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
@@ -82,8 +82,35 @@ describe Resourced::Params do
|
|
82
82
|
inst = klass.new(params, "admin")
|
83
83
|
|
84
84
|
it "should contain conditional param" do
|
85
|
-
inst.
|
85
|
+
inst.attributes.keys.should eq([:a, :b, :c])
|
86
86
|
end
|
87
87
|
end
|
88
88
|
end
|
89
|
+
|
90
|
+
describe "Attribute body" do
|
91
|
+
klass = AttributesTest.dup
|
92
|
+
klass.body :test
|
93
|
+
klass.attributes do
|
94
|
+
allow :a, :b
|
95
|
+
end
|
96
|
+
inst = klass.new({ :test => params })
|
97
|
+
|
98
|
+
it "should get attributes from given body" do
|
99
|
+
inst.attributes.keys.should eq([:a, :b])
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "Coercions" do
|
104
|
+
klass = AttributesTest.dup
|
105
|
+
klass.attributes do
|
106
|
+
allow :a, :as => :boolean
|
107
|
+
allow :b, :as => :integer
|
108
|
+
end
|
109
|
+
inst = klass.new({ :a => "0", :b => "7" })
|
110
|
+
|
111
|
+
it "should coerce params" do
|
112
|
+
inst.attributes[:a].should be_false
|
113
|
+
inst.attributes[:b].should eql(7)
|
114
|
+
end
|
115
|
+
end
|
89
116
|
end
|
@@ -78,4 +78,17 @@ describe Resourced::Finders do
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
+
describe "Default finder" do
|
82
|
+
before :each do
|
83
|
+
klass.default_finder do
|
84
|
+
chain.default("foo")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
let(:params) { {} }
|
88
|
+
|
89
|
+
it "should apply default finder" do
|
90
|
+
inst.apply_finders.chain.result.should eq("#default(foo)")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
81
94
|
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Resourced::Resource do
|
4
|
+
class ResourceModel
|
5
|
+
def initialize
|
6
|
+
@result = ""
|
7
|
+
end
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
def method_missing(name, value)
|
11
|
+
@result += "##{name}(#{value})"
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def title(value)
|
17
|
+
self.new.title(value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def email(value)
|
21
|
+
self.new.email(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class TestResource
|
27
|
+
include Resourced::Resource
|
28
|
+
|
29
|
+
model ResourceModel
|
30
|
+
key :id
|
31
|
+
|
32
|
+
attributes do
|
33
|
+
allow :a, :b
|
34
|
+
allow :c, :if => lambda { scope == :admin }
|
35
|
+
restrict :b, :if => lambda { scope != :admin }
|
36
|
+
end
|
37
|
+
|
38
|
+
finders do
|
39
|
+
finder :title do |v|
|
40
|
+
chain.title(v)
|
41
|
+
end
|
42
|
+
|
43
|
+
finder :email, :if => lambda { scope == :admin } do |v|
|
44
|
+
chain.email(v)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "Context independent properties" do
|
50
|
+
let(:inst) { TestResource.new({}, :admin) }
|
51
|
+
|
52
|
+
it "should store pkey" do
|
53
|
+
inst.key.should eq(:id)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should store model" do
|
57
|
+
inst.model.should eq(ResourceModel)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should store scope" do
|
61
|
+
inst.scope.should eq(:admin)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "Chain" do
|
66
|
+
let(:inst) { TestResource.new({ :title => "John", :email => "test@test.com" }, role) }
|
67
|
+
|
68
|
+
context "when scope not match" do
|
69
|
+
let(:role) { :guest }
|
70
|
+
|
71
|
+
it "should apply finders to the chain" do
|
72
|
+
inst.apply_finders.chain.result.should eq("#title(John)")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should not rewrite model" do
|
76
|
+
inst.apply_finders.model.should eq(ResourceModel)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "when scope is match" do
|
81
|
+
let(:role) { :admin }
|
82
|
+
|
83
|
+
it "should apply finders to the chain" do
|
84
|
+
inst.apply_finders.chain.result.should eq("#title(John)#email(test@test.com)")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "Attributes" do
|
90
|
+
let(:inst) { TestResource.new({ :a => "a", :b => "b", :c => "c" }, :user) }
|
91
|
+
|
92
|
+
it "should filter attributes" do
|
93
|
+
inst.attributes.should eq({ :a => "a"})
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "Finders" do
|
98
|
+
let(:inst) { TestResource.new({ :title => "John", :email => "test@test.com" }) }
|
99
|
+
|
100
|
+
it "should filter finders" do
|
101
|
+
inst.finders.keys.should eq([:title])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resourced
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Andrey Savchenko
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: coercible
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
@@ -57,18 +57,26 @@ files:
|
|
57
57
|
- LICENSE.txt
|
58
58
|
- README.md
|
59
59
|
- Rakefile
|
60
|
+
- lib/generators/rails/resourced_generator.rb
|
61
|
+
- lib/generators/rails/templates/resource.rb
|
62
|
+
- lib/generators/resource_override.rb
|
63
|
+
- lib/generators/rspec/resourced_generator.rb
|
64
|
+
- lib/generators/rspec/templates/resource_spec.rb
|
65
|
+
- lib/generators/test_unit/resourced_generator.rb
|
66
|
+
- lib/generators/test_unit/templates/resource_test.rb
|
60
67
|
- lib/resourced.rb
|
61
68
|
- lib/resourced/active_record.rb
|
62
69
|
- lib/resourced/active_record/actions.rb
|
63
|
-
- lib/resourced/active_record/meta.rb
|
64
70
|
- lib/resourced/active_record/proxy.rb
|
71
|
+
- lib/resourced/attributes.rb
|
65
72
|
- lib/resourced/finders.rb
|
66
|
-
- lib/resourced/
|
73
|
+
- lib/resourced/railtie.rb
|
67
74
|
- lib/resourced/version.rb
|
68
75
|
- resourced.gemspec
|
69
76
|
- spec/resourced/adapters/active_record_spec.rb
|
77
|
+
- spec/resourced/attributes_spec.rb
|
70
78
|
- spec/resourced/finders_spec.rb
|
71
|
-
- spec/resourced/
|
79
|
+
- spec/resourced/resource_spec.rb
|
72
80
|
- spec/spec_helper.rb
|
73
81
|
- spec/support/active_record.rb
|
74
82
|
homepage: https://github.com/Ptico/resourced
|
@@ -86,9 +94,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
95
|
none: false
|
88
96
|
requirements:
|
89
|
-
- - ! '
|
97
|
+
- - ! '>='
|
90
98
|
- !ruby/object:Gem::Version
|
91
|
-
version:
|
99
|
+
version: '0'
|
92
100
|
requirements: []
|
93
101
|
rubyforge_project:
|
94
102
|
rubygems_version: 1.8.23
|
@@ -97,7 +105,8 @@ specification_version: 3
|
|
97
105
|
summary: Missing layer between model and controller
|
98
106
|
test_files:
|
99
107
|
- spec/resourced/adapters/active_record_spec.rb
|
108
|
+
- spec/resourced/attributes_spec.rb
|
100
109
|
- spec/resourced/finders_spec.rb
|
101
|
-
- spec/resourced/
|
110
|
+
- spec/resourced/resource_spec.rb
|
102
111
|
- spec/spec_helper.rb
|
103
112
|
- spec/support/active_record.rb
|