basic_assumption 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY.rdoc +3 -0
- data/README.rdoc +2 -3
- data/lib/basic_assumption.rb +33 -18
- data/lib/basic_assumption/default_assumption.rb +1 -1
- data/lib/basic_assumption/default_assumption/action.rb +56 -0
- data/lib/basic_assumption/default_assumption/class_resolver.rb +19 -13
- data/lib/basic_assumption/default_assumption/name.rb +32 -0
- data/lib/basic_assumption/default_assumption/owner_attributes.rb +36 -0
- data/lib/basic_assumption/default_assumption/rails.rb +109 -25
- data/lib/basic_assumption/rails.rb +2 -3
- data/lib/basic_assumption/version.rb +1 -1
- data/spec/basic_assumption/default_assumption/class_resolver_spec.rb +46 -30
- data/spec/basic_assumption/default_assumption/rails_spec.rb +179 -22
- data/spec/basic_assumption_spec.rb +5 -3
- metadata +50 -41
- data/lib/basic_assumption/default_assumption/restful_rails.rb +0 -112
- data/spec/basic_assumption/default_assumption/restful_rails_spec.rb +0 -280
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bd80407a5479a8d946adf03a45d8247bd6053e2f
|
4
|
+
data.tar.gz: 4c163098fc31e77b897a7c3cbd98bb4a749e7a07
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82613fbd47114b44ef886757e42fd07336faa5d82f48ba2ee73d602652808803bcb5d6302c889bcdda755b06f4fbe722344d2a8522a1d29165665da3f6907cf5
|
7
|
+
data.tar.gz: 33ce020aa383b3c806d641d802475d8e43f1667d3152bcbe48f554d832918400e3e6b204a487a78f866b1c7bf32aebc1e9a7bf7aba84af60e893dd9154afc479
|
data/HISTORY.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -360,10 +360,9 @@ an actual Rails app.
|
|
360
360
|
There is an .rvmrc file in the repository that will require a basic_assumption
|
361
361
|
gemset if you're using RVM, which will help to manage the gem dependencies.
|
362
362
|
|
363
|
-
The test suites are dependent on the Bundler gem.
|
364
|
-
with:
|
363
|
+
The test suites are dependent on the Bundler gem.
|
365
364
|
|
366
|
-
gem install bundler
|
365
|
+
gem install bundler
|
367
366
|
|
368
367
|
It is highly recommended to use RVM to manage development BasicAssumption against
|
369
368
|
various Rails and Ruby versions. Run the following command to create an
|
data/lib/basic_assumption.rb
CHANGED
@@ -62,23 +62,9 @@ module BasicAssumption
|
|
62
62
|
# value returned by the instance method (the reader, from this point of view)
|
63
63
|
# to be overriden.
|
64
64
|
def assume(name, context={}, &block)
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
@basic_assumptions[name] = if block_given?
|
69
|
-
instance_eval(&block)
|
70
|
-
else
|
71
|
-
which = context.delete(:using) || self.class
|
72
|
-
block = DefaultAssumption.resolve(which)
|
73
|
-
instance_exec(name, context, &block)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
@basic_assumptions[name]
|
77
|
-
end
|
78
|
-
define_method("#{name}=") do |value|
|
79
|
-
@basic_assumptions ||= {}
|
80
|
-
@basic_assumptions[name] = value
|
81
|
-
end
|
65
|
+
define_basic_assumptions
|
66
|
+
define_reader_method(name, context, &block)
|
67
|
+
define_writer_method(name)
|
82
68
|
after_assumption(name)
|
83
69
|
end
|
84
70
|
|
@@ -96,8 +82,37 @@ module BasicAssumption
|
|
96
82
|
# end
|
97
83
|
# end
|
98
84
|
def after_assumption(name); end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def define_basic_assumptions
|
89
|
+
define_method(:basic_assumptions) do
|
90
|
+
@basic_assumptions ||= {}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def define_reader_method(name, context, &block)
|
95
|
+
define_method(name) do
|
96
|
+
unless basic_assumptions.key?(name)
|
97
|
+
basic_assumptions[name] = if block_given?
|
98
|
+
instance_eval(&block)
|
99
|
+
else
|
100
|
+
which = context.delete(:using) || self.class
|
101
|
+
block = DefaultAssumption.resolve(which)
|
102
|
+
instance_exec(name, context, &block)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
basic_assumptions[name]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def define_writer_method(name)
|
110
|
+
define_method("#{name}=") do |value|
|
111
|
+
basic_assumptions[name] = value
|
112
|
+
end
|
113
|
+
end
|
99
114
|
end
|
100
115
|
|
101
|
-
if defined?(Rails) && Rails::VERSION::MAJOR
|
116
|
+
if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
102
117
|
require 'basic_assumption/rails'
|
103
118
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module BasicAssumption
|
2
|
+
module DefaultAssumption
|
3
|
+
class Action
|
4
|
+
attr_reader :request, :callbacks
|
5
|
+
|
6
|
+
def initialize(request, &block)
|
7
|
+
@request = request
|
8
|
+
@callbacks = {}
|
9
|
+
|
10
|
+
block.yield self
|
11
|
+
end
|
12
|
+
|
13
|
+
%w(new update find index).each do |name|
|
14
|
+
class_eval <<-MET, __FILE__, __LINE__
|
15
|
+
def #{name}(&block)
|
16
|
+
if block_given?
|
17
|
+
callbacks[:#{name}] = block
|
18
|
+
else
|
19
|
+
(callbacks[:#{name}] || callbacks[:default]).call
|
20
|
+
end
|
21
|
+
end
|
22
|
+
MET
|
23
|
+
end
|
24
|
+
|
25
|
+
def default(&block)
|
26
|
+
callbacks[:default] = block
|
27
|
+
end
|
28
|
+
|
29
|
+
def outcome
|
30
|
+
return index if index?
|
31
|
+
return new if make?
|
32
|
+
return update if update?
|
33
|
+
find
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def index?
|
39
|
+
action == 'index'
|
40
|
+
end
|
41
|
+
|
42
|
+
def make? #:nodoc:
|
43
|
+
%w(new create).include?(action)
|
44
|
+
end
|
45
|
+
|
46
|
+
def update?
|
47
|
+
%w(update edit).include?(action)
|
48
|
+
end
|
49
|
+
|
50
|
+
def action
|
51
|
+
request.params['action']
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -1,22 +1,28 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
1
3
|
module BasicAssumption
|
2
4
|
module DefaultAssumption
|
3
5
|
class ClassResolver #:nodoc:
|
4
|
-
|
5
|
-
|
6
|
+
include ActiveSupport::Inflector
|
7
|
+
|
8
|
+
attr_reader :name, :namespace
|
9
|
+
|
10
|
+
def initialize(name, namespace='')
|
11
|
+
@name, @namespace = name, namespace
|
6
12
|
end
|
7
|
-
|
8
|
-
|
13
|
+
|
14
|
+
def klass
|
15
|
+
@klass ||= constantize(class_name_in_namespace)
|
9
16
|
end
|
10
|
-
|
11
|
-
|
17
|
+
|
18
|
+
def instance
|
19
|
+
klass.new
|
12
20
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def
|
17
|
-
namespace
|
18
|
-
namespace.const_missing(name) unless namespace.const_defined? name
|
19
|
-
namespace.const_get(name)
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def class_name_in_namespace
|
25
|
+
"#{namespace}::#{camelize(name)}"
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
|
3
|
+
module BasicAssumption
|
4
|
+
module DefaultAssumption
|
5
|
+
class Name
|
6
|
+
include ActiveSupport::Inflector
|
7
|
+
|
8
|
+
attr_reader :name
|
9
|
+
|
10
|
+
def initialize(name)
|
11
|
+
@name = name.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def singular
|
15
|
+
singularize(name)
|
16
|
+
end
|
17
|
+
|
18
|
+
def plural
|
19
|
+
pluralize(name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def plural?
|
23
|
+
plural.eql?(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
singular
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module BasicAssumption
|
2
|
+
module DefaultAssumption
|
3
|
+
class OwnerAttributes
|
4
|
+
attr_reader :controller, :owner_context, :owner_method
|
5
|
+
|
6
|
+
def initialize(owner_method_or_context, controller)
|
7
|
+
@controller = controller
|
8
|
+
|
9
|
+
if owner_method_or_context.kind_of? Hash
|
10
|
+
@owner_method = owner_method_or_context[:object]
|
11
|
+
@owner_context = owner_method_or_context
|
12
|
+
else
|
13
|
+
@owner_method = owner_method_or_context
|
14
|
+
@owner_context = {}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def attributes
|
19
|
+
{column_name => owner_object.id}
|
20
|
+
end
|
21
|
+
|
22
|
+
def owner_object
|
23
|
+
if owner_method.respond_to? :call
|
24
|
+
controller.instance_eval(&owner_method)
|
25
|
+
else
|
26
|
+
controller.send(owner_method)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def column_name
|
31
|
+
owner_context[:column_name] || :"#{owner_object.class.name.downcase}_id"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -1,15 +1,56 @@
|
|
1
|
+
require 'basic_assumption/default_assumption/action'
|
2
|
+
require 'basic_assumption/default_assumption/class_resolver'
|
3
|
+
require 'basic_assumption/default_assumption/name'
|
4
|
+
require 'basic_assumption/default_assumption/owner_attributes'
|
5
|
+
|
1
6
|
module BasicAssumption
|
2
7
|
module DefaultAssumption
|
3
|
-
#
|
4
|
-
class Rails
|
5
|
-
attr_reader :
|
8
|
+
# Restful default behavior in the context of Rails
|
9
|
+
class Rails
|
10
|
+
attr_reader :action, :context, :name, :request #:nodoc:
|
11
|
+
|
12
|
+
def initialize(name=nil, context={}, request=nil) #:nodoc:
|
13
|
+
@context = context
|
14
|
+
@name = Name.new(context.delete(:as) || name)
|
15
|
+
@request = request
|
6
16
|
|
7
|
-
|
8
|
-
@name, @context, @params = name.to_s, context, params
|
17
|
+
@action = initialize_action
|
9
18
|
end
|
10
|
-
|
11
|
-
#
|
12
|
-
#
|
19
|
+
|
20
|
+
# Returns a block that will attempt to do the correct thing depending
|
21
|
+
# on the plurality of the name passed to +assume+ and the action for the
|
22
|
+
# current request. If the name is singular and the action is not 'new'
|
23
|
+
# or 'create', then +assume+ will find an instance of
|
24
|
+
# an ActiveRecord model of the name that it received and an id
|
25
|
+
# value in the parameters. If the action is 'new' or 'create', +assume+
|
26
|
+
# will instantiate a new instance of the model class, passing in the
|
27
|
+
# values it finds in the +params+ hash with for a key of the name passed
|
28
|
+
# to +assume+. For example:
|
29
|
+
#
|
30
|
+
# class WidgetController < ApplicationController
|
31
|
+
# default_assumption :rails
|
32
|
+
# assume :widget
|
33
|
+
#
|
34
|
+
# def create
|
35
|
+
# widget.save! # widget is: Widget.new(params[:widget])
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# Note the object will have been instantiated but not saved, destroyed,
|
40
|
+
# etc.
|
41
|
+
#
|
42
|
+
# If the name passed to assume is plural, +assume+ returns all records
|
43
|
+
# for the model.
|
44
|
+
#
|
45
|
+
# It is possible to specify an alternative model name:
|
46
|
+
#
|
47
|
+
# class WidgetController < ApplicationController
|
48
|
+
# assume :sprocket, :as => :widget
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# This will create a +sprocket+ method in your actions and view
|
52
|
+
# that will use the Widget model for its lookup.
|
53
|
+
#
|
13
54
|
# The following two examples would be equivalent:
|
14
55
|
#
|
15
56
|
# class WidgetController < ActionController::Base
|
@@ -46,6 +87,7 @@ module BasicAssumption
|
|
46
87
|
# configuration options, such as:
|
47
88
|
#
|
48
89
|
# conf.active_record.raise_error = true
|
90
|
+
# conf.active_record.find_on_id = true
|
49
91
|
#
|
50
92
|
# It is possible to specify an alternative model name:
|
51
93
|
#
|
@@ -56,36 +98,78 @@ module BasicAssumption
|
|
56
98
|
# This will create a +sprocket+ method in your actions and view
|
57
99
|
# that will use the Widget model for its lookup.
|
58
100
|
def block
|
59
|
-
|
101
|
+
default = self
|
60
102
|
Proc.new do |name, context|
|
61
|
-
|
103
|
+
context[:controller] = self
|
104
|
+
|
105
|
+
default.class.new(name, context, request).result
|
62
106
|
end
|
63
107
|
end
|
64
108
|
|
65
109
|
def result #:nodoc:
|
66
|
-
|
67
|
-
model_class.find(lookup_id)
|
68
|
-
rescue
|
69
|
-
raise if settings[:raise_error]
|
70
|
-
nil
|
71
|
-
end
|
110
|
+
action.outcome
|
72
111
|
end
|
73
112
|
|
74
113
|
protected
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
114
|
+
|
115
|
+
def initialize_action
|
116
|
+
name.plural? ? plural_action : singular_action
|
117
|
+
end
|
118
|
+
|
119
|
+
def singular_action
|
120
|
+
Action.new(request) do |action|
|
121
|
+
action.find do
|
122
|
+
find_and_maybe_raise
|
123
|
+
end
|
124
|
+
action.update do
|
125
|
+
record = find_and_maybe_raise
|
126
|
+
record.attributes = resource_attributes
|
127
|
+
record
|
128
|
+
end
|
129
|
+
action.default do
|
130
|
+
model_class.new(resource_attributes.merge(owner_attributes))
|
131
|
+
end
|
80
132
|
end
|
81
133
|
end
|
82
134
|
|
83
|
-
def
|
84
|
-
|
135
|
+
def plural_action
|
136
|
+
Action.new(request) do |action|
|
137
|
+
action.default do
|
138
|
+
finder_scope
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def finder_scope
|
144
|
+
model_class.where(owner_attributes)
|
145
|
+
end
|
146
|
+
|
147
|
+
def model_class
|
148
|
+
@model_class ||= ClassResolver.new(name).klass
|
149
|
+
end
|
150
|
+
|
151
|
+
def params
|
152
|
+
@params ||= request ? request.params : {}
|
85
153
|
end
|
86
154
|
|
87
|
-
def
|
88
|
-
|
155
|
+
def find_and_maybe_raise
|
156
|
+
begin
|
157
|
+
finder_scope.find(params['id'])
|
158
|
+
rescue
|
159
|
+
raise if settings[:raise_error]
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def resource_attributes
|
164
|
+
params[name.singular] || {}
|
165
|
+
end
|
166
|
+
|
167
|
+
def owner_attributes
|
168
|
+
@owner_attributes ||= if context[:owner]
|
169
|
+
OwnerAttributes.new(context[:owner], context[:controller]).attributes
|
170
|
+
else
|
171
|
+
{}
|
172
|
+
end
|
89
173
|
end
|
90
174
|
|
91
175
|
def settings #:nodoc:
|
@@ -1,6 +1,5 @@
|
|
1
|
-
if defined?(Rails) && Rails::VERSION::MAJOR
|
1
|
+
if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
2
2
|
require 'basic_assumption/default_assumption/rails'
|
3
|
-
require 'basic_assumption/default_assumption/restful_rails'
|
4
3
|
|
5
4
|
module BasicAssumption
|
6
5
|
# Must be required explicitly in Rails 3. Extends ActionController::Base
|
@@ -16,7 +15,7 @@ if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
|
16
15
|
# by +assume+ as a helper inside of views, and also disallows the
|
17
16
|
# method from being called directly as a route endpoint.
|
18
17
|
def self.after_assumption(name)
|
19
|
-
hide_action
|
18
|
+
hide_action name
|
20
19
|
helper_method name
|
21
20
|
end
|
22
21
|
end
|