basic_assumption 0.4.1 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.rdoc +13 -0
- data/README.rdoc +41 -37
- data/lib/basic_assumption.rb +9 -6
- data/lib/basic_assumption/configuration.rb +18 -10
- data/lib/basic_assumption/configuration/active_record.rb +19 -0
- data/lib/basic_assumption/default_assumption.rb +4 -3
- data/lib/basic_assumption/default_assumption/rails.rb +44 -3
- data/lib/basic_assumption/rails.rb +0 -1
- data/lib/basic_assumption/version.rb +1 -1
- data/spec/basic_assumption/configuration_spec.rb +63 -0
- data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/base_spec.rb +0 -0
- data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/class_resolver_spec.rb +0 -0
- data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/rails_spec.rb +14 -4
- data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/restful_rails_spec.rb +0 -0
- data/spec/{lib/basic_assumption → basic_assumption}/default_assumption_spec.rb +0 -0
- data/spec/{lib/basic_assumption_spec.rb → basic_assumption_spec.rb} +12 -7
- data/spec/spec_helper.rb +22 -0
- metadata +24 -36
- data/lib/basic_assumption/default_assumption/cautious_rails.rb +0 -40
- data/spec/lib/basic_assumption/configuration_spec.rb +0 -33
- data/spec/lib/basic_assumption/default_assumption/cautious_rails_spec.rb +0 -40
data/HISTORY.rdoc
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
== 0.5.1 / 2011-10-05
|
2
|
+
* Bug Fix
|
3
|
+
* Included missing file in gem package
|
4
|
+
== 0.5.0 / 2011-10-04 - YANKED, incorrectly published as 5.0.0
|
5
|
+
* Feature Change
|
6
|
+
* Default Rails assumption is now "cautious"; use find_on_id and/or raise_error for previous behavior
|
7
|
+
* emulate_exposure! has been removed and replaced by alias_assume_to, which only partially stands in
|
8
|
+
* Bug Fix
|
9
|
+
* Assumptions can be overridden by nil (such as within a controller action)
|
10
|
+
* Reloading classes that include BasicAssumption (a la Rails controllers in dev mode) doesn't cause a leak
|
11
|
+
== 0.4.1 / 2010-07-16
|
12
|
+
* Feature Change
|
13
|
+
* No longer necessary to require 'basic_assumption/rails' in Gemfile
|
1
14
|
== 0.4.0 / 2010-07-16
|
2
15
|
* Feature Addition
|
3
16
|
* Add simple rspec matcher
|
data/README.rdoc
CHANGED
@@ -1,21 +1,9 @@
|
|
1
1
|
== BasicAssumption
|
2
2
|
|
3
3
|
BasicAssumption is a gem that lets you declare resources inside of a class
|
4
|
-
in a concise manner.
|
5
|
-
|
6
|
-
|
7
|
-
providing custom defaults and some useful defaults out of the box.
|
8
|
-
|
9
|
-
=== What is DecentExposure?
|
10
|
-
|
11
|
-
It's a plugin written by {Stephen Caudill}[http://voxdolo.me/] that provides a way of
|
12
|
-
cleaning up Rails controller and view code. Find it at
|
13
|
-
{its GitHub repository}[http://github.com/voxdolo/decent_exposure].
|
14
|
-
|
15
|
-
DecentExposure coins an idiom for writing certain kinds of code in a declarative
|
16
|
-
way. Particularly for your Rails apps, it's worth checking out. BasicAssumption
|
17
|
-
is another implementation of the idiom. Check the BasicAssumption::Configuration
|
18
|
-
options for enabling compatibility with the DecentExposure API.
|
4
|
+
in a concise manner. It implements an idiom for writing certain kinds of code
|
5
|
+
in a declarative way. In particular, it's meant to make Rails controllers and
|
6
|
+
views cleaner.
|
19
7
|
|
20
8
|
== Install BasicAssumption
|
21
9
|
|
@@ -31,12 +19,7 @@ For Rails 2, in environment.rb:
|
|
31
19
|
|
32
20
|
For Rails 3, in your Gemfile:
|
33
21
|
|
34
|
-
gem 'basic_assumption'
|
35
|
-
|
36
|
-
It's possible to use BasicAssumption in your Rails 3 app without requiring
|
37
|
-
'basic_assumption/rails', and will in fact allow more flexibility if you don't,
|
38
|
-
but will incur the cost of additional setup. See the file
|
39
|
-
'lib/basic_assumption/rails.rb' for guidance.
|
22
|
+
gem 'basic_assumption'
|
40
23
|
|
41
24
|
To use the library in another context, it is enough to extend the
|
42
25
|
BasicAssumption module inside the class you would like it available.
|
@@ -148,7 +131,7 @@ For example, this:
|
|
148
131
|
|
149
132
|
protected
|
150
133
|
def find_record
|
151
|
-
@record = Record.find(params[:record_id]
|
134
|
+
@record = Record.find(params[:record_id])
|
152
135
|
end
|
153
136
|
end
|
154
137
|
|
@@ -231,7 +214,7 @@ to override the name of the model that is being worked with via the :as option.
|
|
231
214
|
Here is an example:
|
232
215
|
|
233
216
|
class Widget < ActiveRecord::Base
|
234
|
-
named_scope :shiny, :
|
217
|
+
named_scope :shiny, where(:glossy => true)
|
235
218
|
end
|
236
219
|
|
237
220
|
class WidgetController < ActionController::Base
|
@@ -255,15 +238,22 @@ following two constructs would be equivalent in your controllers:
|
|
255
238
|
# The above line is exactly the same as:
|
256
239
|
assume :film
|
257
240
|
|
258
|
-
Please see +Rails+ for implementation details
|
259
|
-
|
260
|
-
|
261
|
-
|
241
|
+
Please see +Rails+ for implementation details. Though it could be considered
|
242
|
+
a bit more dangerous to do, this standard Rails default will accept an option
|
243
|
+
:find_on_id, that will find on params[:id] as well as params[:name_id].
|
244
|
+
Enable that for one of your controllers like so:
|
262
245
|
|
263
246
|
class FilmController < ActionController::Base
|
264
|
-
|
247
|
+
assume :film, :find_on_id => true
|
265
248
|
end
|
266
249
|
|
250
|
+
It's also possible to have this behavior turned on by default via a
|
251
|
+
configuration setting, which may be convenient for backwards compatibility
|
252
|
+
with versions of BasicAssumption prior to 0.5.0. Similarly, there is a
|
253
|
+
raise_error setting that will cause any errors that result from the attempt
|
254
|
+
to find the record to bubble up; otherwise, they will be swallowed and the
|
255
|
+
assumption method will return nil.
|
256
|
+
|
267
257
|
Another option is :restful_rails, which attempts to provide appropriate
|
268
258
|
behavior for the basic RESTful actions. Please see +RestfulRails+ for a
|
269
259
|
description of how it works.
|
@@ -324,9 +314,16 @@ implications of evaluating the +Proc+ returned by +block+ using +instance_eval+
|
|
324
314
|
|
325
315
|
There are a couple of configuration settings that can be set inside of
|
326
316
|
a configuration block that can be used in places such as Rails initializer
|
327
|
-
blocks. #
|
328
|
-
+expose+ and +
|
329
|
-
default behavior. For more information, see
|
317
|
+
blocks. #alias_assume_to will alias +assume+ to other names. The example
|
318
|
+
below would alias it to +expose+ and +reveal+. You can also set the
|
319
|
+
app-wide default behavior. For more information, see
|
320
|
+
BasicAssumption::Configuration.
|
321
|
+
|
322
|
+
BasicAssumption::Configuration.configure do |conf|
|
323
|
+
conf.default_assumption = Proc.new { "I <3 GitHub." }
|
324
|
+
|
325
|
+
conf.alias_assume_to :expose, :reveal
|
326
|
+
end
|
330
327
|
|
331
328
|
== Issues to note
|
332
329
|
|
@@ -366,11 +363,18 @@ gemset if you're using RVM, which will help to manage the gem dependencies.
|
|
366
363
|
The test suites are dependent on the Bundler gem. Ensure that it is installed
|
367
364
|
with:
|
368
365
|
|
369
|
-
gem install bundler
|
366
|
+
gem install bundler --version="~> 1.0.3"
|
367
|
+
|
368
|
+
It is highly recommended to use RVM to manage development BasicAssumption against
|
369
|
+
various Rails and Ruby versions. Run the following command to create an
|
370
|
+
appropriate RVM gemset and receive a command to run manually that selects that
|
371
|
+
gemset:
|
372
|
+
|
373
|
+
rake rvm:gemset
|
370
374
|
|
371
375
|
To run the Cucumber and spec suites for the first time, use these Rake tasks:
|
372
376
|
|
373
|
-
rake init
|
377
|
+
rake init:rails2 #or rake init:rails3
|
374
378
|
rake
|
375
379
|
|
376
380
|
Note that the +init+ task will +bundle+ +install+ the development dependencies,
|
@@ -385,9 +389,9 @@ Feel free to fork away and send back pull requests, including specs! Thanks.
|
|
385
389
|
== But should I use it?
|
386
390
|
|
387
391
|
Sure! Absolutely. I think it's a cool idea that lets you cut down on line
|
388
|
-
noise, particularly in your Rails controllers. You may want to
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
+
noise, particularly in your Rails controllers. You may also want to look at
|
393
|
+
{DecentExposure}[http://github.com/voxdolo/decent_exposure], the
|
394
|
+
project BasicAssumption is based on, written by {Stephen Caudill}[http://voxdolo.me/]
|
395
|
+
of {Hashrocket}[http://www.hashrocket.com/]. Feel free to let me know
|
392
396
|
if you use it! Email mby [at] mattyoho [dot] com with questions, comments, or
|
393
397
|
non-sequiters.
|
data/lib/basic_assumption.rb
CHANGED
@@ -64,13 +64,16 @@ module BasicAssumption
|
|
64
64
|
def assume(name, context={}, &block)
|
65
65
|
define_method(name) do
|
66
66
|
@basic_assumptions ||= {}
|
67
|
-
@basic_assumptions
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
unless @basic_assumptions.key?(name)
|
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
|
73
75
|
end
|
76
|
+
@basic_assumptions[name]
|
74
77
|
end
|
75
78
|
define_method("#{name}=") do |value|
|
76
79
|
@basic_assumptions ||= {}
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'basic_assumption/configuration/active_record'
|
2
|
+
|
1
3
|
module BasicAssumption
|
2
4
|
# Provides app-level configuration options for +BasicAssumption+.
|
3
5
|
# Useful in a Rails initializer or something similar.
|
@@ -7,18 +9,24 @@ module BasicAssumption
|
|
7
9
|
# conf.default_assumption = Proc.new { "I <3 GitHub." }
|
8
10
|
# end
|
9
11
|
def self.configure #:yields: config_instance
|
10
|
-
|
12
|
+
@configuration = self.new
|
13
|
+
yield @configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.settings
|
17
|
+
@configuration.active_record.settings_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :active_record #:nodoc:
|
21
|
+
|
22
|
+
def initialize #:nodoc:
|
23
|
+
@active_record = self.class::ActiveRecord.new
|
11
24
|
end
|
12
25
|
|
13
|
-
#
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
# identically to +assume+ and +default_assumption+ (or rather, vice-versa.)
|
18
|
-
def emulate_exposure!
|
19
|
-
BasicAssumption.module_eval do
|
20
|
-
alias expose assume
|
21
|
-
alias default_exposure default_assumption
|
26
|
+
# Allows substituting another method name aside from +assume+
|
27
|
+
def alias_assume_to(*aliases)
|
28
|
+
aliases.each do |a|
|
29
|
+
BasicAssumption.module_eval "alias #{a} assume"
|
22
30
|
end
|
23
31
|
end
|
24
32
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module BasicAssumption
|
2
|
+
class Configuration
|
3
|
+
class ActiveRecord #:nodoc:
|
4
|
+
SETTINGS = [:find_on_id, :raise_error]
|
5
|
+
|
6
|
+
SETTINGS.each do |setting|
|
7
|
+
attr_accessor setting
|
8
|
+
end
|
9
|
+
|
10
|
+
def settings_hash
|
11
|
+
SETTINGS.inject({}) do |hash, setting|
|
12
|
+
hash[setting] = self.send setting
|
13
|
+
hash
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -29,16 +29,17 @@ module BasicAssumption
|
|
29
29
|
# behavior. See the +Rails+ class for an example.
|
30
30
|
module DefaultAssumption
|
31
31
|
def self.register(klass, default) #:nodoc:
|
32
|
-
registry[klass.
|
32
|
+
registry[klass.name] = strategy(default)
|
33
33
|
end
|
34
34
|
|
35
35
|
def self.resolve(klass) #:nodoc:
|
36
36
|
return strategy(klass) if klass.kind_of?(Symbol)
|
37
|
-
while !registry.has_key?(klass.
|
37
|
+
while !registry.has_key?(klass.name)
|
38
38
|
klass = superclass(klass)
|
39
39
|
break if klass.nil?
|
40
40
|
end
|
41
|
-
|
41
|
+
lookup = klass && klass.name
|
42
|
+
registry[lookup]
|
42
43
|
end
|
43
44
|
|
44
45
|
class << self
|
@@ -17,9 +17,36 @@ module BasicAssumption
|
|
17
17
|
# end
|
18
18
|
#
|
19
19
|
# class WidgetController < ActionController::Base
|
20
|
-
# assume(:widget) { Widget.find(params[:widget_id]
|
20
|
+
# assume(:widget) { Widget.find(params[:widget_id]) rescue nil }
|
21
21
|
# end
|
22
22
|
#
|
23
|
+
# The find can also fall back to using params[:id] when
|
24
|
+
# :find_on_id is specified. The following are equivalent:
|
25
|
+
#
|
26
|
+
# class WidgetController < ActionController::Base
|
27
|
+
# assume :widget, :find_on_id => true
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# class WidgetController < ActionController::Base
|
31
|
+
# assume(:widget) { Widget.find(params[:widget_id] || params[:id]) rescue nil }
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# The find will, by default, swallow errors encountered
|
35
|
+
# when finding. This can be overridden by setting :raise_error.
|
36
|
+
#
|
37
|
+
# class WidgetController < ActionController::Base
|
38
|
+
# assume :widget, :raise_error => true
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# class WidgetController < ActionController::Base
|
42
|
+
# assume(:widget) { Widget.find(params[:widget_id]) }
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# Both of these settings can be turned on by default via
|
46
|
+
# configuration options, such as:
|
47
|
+
#
|
48
|
+
# conf.active_record.raise_error = true
|
49
|
+
#
|
23
50
|
# It is possible to specify an alternative model name:
|
24
51
|
#
|
25
52
|
# class WidgetController < ApplicationController
|
@@ -36,12 +63,21 @@ module BasicAssumption
|
|
36
63
|
end
|
37
64
|
|
38
65
|
def result #:nodoc:
|
39
|
-
|
66
|
+
begin
|
67
|
+
model_class.find(lookup_id)
|
68
|
+
rescue
|
69
|
+
raise if settings[:raise_error]
|
70
|
+
nil
|
71
|
+
end
|
40
72
|
end
|
41
73
|
|
42
74
|
protected
|
43
75
|
def lookup_id #:nodoc:
|
44
|
-
|
76
|
+
if settings[:find_on_id]
|
77
|
+
params["#{name}_id"] || params['id']
|
78
|
+
else
|
79
|
+
params["#{name}_id"]
|
80
|
+
end
|
45
81
|
end
|
46
82
|
|
47
83
|
def model_class #:nodoc:
|
@@ -51,6 +87,11 @@ module BasicAssumption
|
|
51
87
|
def model_name #:nodoc:
|
52
88
|
context[:as] ? context[:as].to_s : name
|
53
89
|
end
|
90
|
+
|
91
|
+
def settings #:nodoc:
|
92
|
+
@global_settings ||= BasicAssumption::Configuration.settings
|
93
|
+
@global_settings.merge(context)
|
94
|
+
end
|
54
95
|
end
|
55
96
|
end
|
56
97
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BasicAssumption::Configuration do
|
4
|
+
it "provides a #configure class method that yields an instance of the class" do
|
5
|
+
configuration_instance = nil
|
6
|
+
BasicAssumption::Configuration.configure { |instance| configuration_instance = instance }
|
7
|
+
configuration_instance.should be_a_kind_of(BasicAssumption::Configuration)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe ".settings" do
|
11
|
+
it "returns a hash of the current settings" do
|
12
|
+
BasicAssumption::Configuration.configure do |config|
|
13
|
+
config.active_record.raise_error = true
|
14
|
+
end
|
15
|
+
|
16
|
+
settings = BasicAssumption::Configuration.settings
|
17
|
+
settings.should have_key(:raise_error)
|
18
|
+
settings[:raise_error].should be_true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "an instance" do
|
23
|
+
class Assumer
|
24
|
+
extend BasicAssumption
|
25
|
+
end
|
26
|
+
let(:config) { BasicAssumption::Configuration.new }
|
27
|
+
let(:assuming_class) { named_class_extending Assumer }
|
28
|
+
|
29
|
+
describe "#alias_assume_to" do
|
30
|
+
it "aliases the #assume method" do
|
31
|
+
config.alias_assume_to :expose, :provide
|
32
|
+
assuming_class.should respond_to(:expose)
|
33
|
+
assuming_class.should respond_to(:provide)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "can set the default assumption" do
|
38
|
+
config.default_assumption = Proc.new { :qux }
|
39
|
+
assuming_class.class_eval do
|
40
|
+
assume :baz
|
41
|
+
end
|
42
|
+
assuming_class.new.baz.should eql(:qux)
|
43
|
+
end
|
44
|
+
|
45
|
+
context "active_record" do
|
46
|
+
it "has a raise_error setting" do
|
47
|
+
expect do
|
48
|
+
config.active_record.raise_error = true
|
49
|
+
end.to_not raise_error
|
50
|
+
end
|
51
|
+
|
52
|
+
it "has a find_on_id setting" do
|
53
|
+
expect do
|
54
|
+
config.active_record.find_on_id = true
|
55
|
+
end.to_not raise_error
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
after(:all) do
|
60
|
+
config.default_assumption = nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
File without changes
|
data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/class_resolver_spec.rb
RENAMED
File without changes
|
@@ -15,10 +15,20 @@ describe BasicAssumption::DefaultAssumption::Rails do
|
|
15
15
|
default.stub!(:params).and_return(params)
|
16
16
|
end
|
17
17
|
|
18
|
-
|
19
|
-
params
|
20
|
-
|
21
|
-
|
18
|
+
context "when context[:find_on_id] is true" do
|
19
|
+
it "looks for a params[model_id] and params[id] in its calling context" do
|
20
|
+
params.should_receive(:[]).with('model_id').and_return(nil)
|
21
|
+
params.should_receive(:[]).with('id')
|
22
|
+
default.block.call(:model, {:find_on_id => true})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when context[:find_on_id] is not true" do
|
27
|
+
it "looks for a params[model_id] in its calling context" do
|
28
|
+
params.should_receive(:[]).with('model_id').and_return(nil)
|
29
|
+
params.should_not_receive(:[]).with('id')
|
30
|
+
default.block.call(:model, {:find_on_id => nil})
|
31
|
+
end
|
22
32
|
end
|
23
33
|
|
24
34
|
it "attempts to find a model instance based off the given name" do
|
data/spec/{lib/basic_assumption → basic_assumption}/default_assumption/restful_rails_spec.rb
RENAMED
File without changes
|
File without changes
|
@@ -11,7 +11,7 @@ describe BasicAssumption do
|
|
11
11
|
|
12
12
|
context "when a class extends BasicAssumption" do
|
13
13
|
|
14
|
-
let(:extender_class)
|
14
|
+
let(:extender_class) { named_class_extending Extender }
|
15
15
|
let(:extender_instance) { extender_class.new }
|
16
16
|
before(:each) do
|
17
17
|
extender_class.extend(BasicAssumption)
|
@@ -125,14 +125,19 @@ describe BasicAssumption do
|
|
125
125
|
extender_instance.writeable = 'overwritten'
|
126
126
|
extender_instance.writeable.should eql('overwritten')
|
127
127
|
end
|
128
|
+
|
129
|
+
it "overrides the value returned by the created instance method with nil" do
|
130
|
+
extender_instance.writeable = nil
|
131
|
+
extender_instance.writeable.should be_nil
|
132
|
+
end
|
128
133
|
end
|
129
134
|
end
|
130
135
|
|
131
136
|
context "within Rails" do
|
132
137
|
before(:all) do
|
133
|
-
require File.expand_path(File.dirname(__FILE__) + '
|
138
|
+
require File.expand_path(File.dirname(__FILE__) + '/../rails/init.rb')
|
134
139
|
end
|
135
|
-
let(:controller_class)
|
140
|
+
let(:controller_class) { named_class_extending(::ActionController::Base) }
|
136
141
|
let(:controller_instance) { controller_class.new }
|
137
142
|
|
138
143
|
it "is extended by ActionController::Base" do
|
@@ -156,9 +161,9 @@ describe BasicAssumption do
|
|
156
161
|
end
|
157
162
|
|
158
163
|
context "classes in the inheritance chain of ActionController::Base" do
|
159
|
-
let(:application_controller) {
|
160
|
-
let(:derived_class)
|
161
|
-
let(:derived_instance)
|
164
|
+
let(:application_controller) { named_class_extending controller_class }
|
165
|
+
let(:derived_class) { named_class_extending application_controller }
|
166
|
+
let(:derived_instance) { derived_class.new }
|
162
167
|
|
163
168
|
before(:all) do
|
164
169
|
application_controller.class_eval do
|
@@ -180,7 +185,7 @@ describe BasicAssumption do
|
|
180
185
|
controller_class.class_eval do
|
181
186
|
assume(:model)
|
182
187
|
end
|
183
|
-
controller_instance.stub(:params => {'
|
188
|
+
controller_instance.stub(:params => {'model_id' => 123})
|
184
189
|
::Model.should_receive(:find).with(123)
|
185
190
|
controller_instance.model.should be_a_kind_of(Object)
|
186
191
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,25 @@
|
|
1
1
|
require 'bundler'
|
2
2
|
Bundler.setup
|
3
3
|
require 'basic_assumption'
|
4
|
+
|
5
|
+
module SubclassContainer
|
6
|
+
def next_subclass_id
|
7
|
+
@subclass_count ||= 0
|
8
|
+
@subclass_count += 1
|
9
|
+
end
|
10
|
+
extend self
|
11
|
+
end
|
12
|
+
|
13
|
+
module BasicAssumptionSpecHelpers
|
14
|
+
def named_class_extending(base)
|
15
|
+
extender = Class.new(base)
|
16
|
+
subclass_name = "Subclass_#{SubclassContainer.next_subclass_id}_#{base.name.gsub(/:+/, '_')}"
|
17
|
+
SubclassContainer.const_set subclass_name, extender
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.include(BasicAssumptionSpecHelpers)
|
23
|
+
|
24
|
+
config.mock_with :rspec
|
25
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_assumption
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 4
|
8
|
-
- 1
|
9
|
-
version: 0.4.1
|
4
|
+
prerelease:
|
5
|
+
version: 0.5.1
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Matt Yoho
|
@@ -14,23 +10,10 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-10-05 00:00:00 -04:00
|
18
14
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
name: bundler
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
|
-
- 9
|
30
|
-
- 26
|
31
|
-
version: 0.9.26
|
32
|
-
type: :development
|
33
|
-
version_requirements: *id001
|
15
|
+
dependencies: []
|
16
|
+
|
34
17
|
description: "\n Allows a simple declarative idiom for accessing resources in controllers and views\n via a well-defined interface that increases testability and reduces shared state.\n "
|
35
18
|
email: mby@mattyoho.com
|
36
19
|
executables: []
|
@@ -47,9 +30,9 @@ files:
|
|
47
30
|
- README.rdoc
|
48
31
|
- lib/basic_assumption.rb
|
49
32
|
- lib/basic_assumption/configuration.rb
|
33
|
+
- lib/basic_assumption/configuration/active_record.rb
|
50
34
|
- lib/basic_assumption/default_assumption.rb
|
51
35
|
- lib/basic_assumption/default_assumption/base.rb
|
52
|
-
- lib/basic_assumption/default_assumption/cautious_rails.rb
|
53
36
|
- lib/basic_assumption/default_assumption/class_resolver.rb
|
54
37
|
- lib/basic_assumption/default_assumption/rails.rb
|
55
38
|
- lib/basic_assumption/default_assumption/restful_rails.rb
|
@@ -57,6 +40,14 @@ files:
|
|
57
40
|
- lib/basic_assumption/rspec.rb
|
58
41
|
- lib/basic_assumption/version.rb
|
59
42
|
- rails/init.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- spec/basic_assumption_spec.rb
|
45
|
+
- spec/basic_assumption/configuration_spec.rb
|
46
|
+
- spec/basic_assumption/default_assumption_spec.rb
|
47
|
+
- spec/basic_assumption/default_assumption/base_spec.rb
|
48
|
+
- spec/basic_assumption/default_assumption/class_resolver_spec.rb
|
49
|
+
- spec/basic_assumption/default_assumption/rails_spec.rb
|
50
|
+
- spec/basic_assumption/default_assumption/restful_rails_spec.rb
|
60
51
|
has_rdoc: true
|
61
52
|
homepage: http://github.com/mattyoho/basic_assumption
|
62
53
|
licenses: []
|
@@ -67,33 +58,30 @@ rdoc_options:
|
|
67
58
|
require_paths:
|
68
59
|
- lib
|
69
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
70
62
|
requirements:
|
71
63
|
- - ">="
|
72
64
|
- !ruby/object:Gem::Version
|
73
|
-
segments:
|
74
|
-
- 0
|
75
65
|
version: "0"
|
76
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
77
68
|
requirements:
|
78
69
|
- - ">="
|
79
70
|
- !ruby/object:Gem::Version
|
80
|
-
segments:
|
81
|
-
- 0
|
82
71
|
version: "0"
|
83
72
|
requirements: []
|
84
73
|
|
85
74
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.
|
75
|
+
rubygems_version: 1.6.2
|
87
76
|
signing_key:
|
88
77
|
specification_version: 3
|
89
78
|
summary: Allows a simple declarative idiom for accessing resources in controllers and views, cleaning up controller code and removing the need to explicitly reference instance variables inside views. Custom default behavior can be defined in a pluggable manner.
|
90
79
|
test_files:
|
91
80
|
- spec/spec_helper.rb
|
92
|
-
- spec/
|
93
|
-
- spec/
|
94
|
-
- spec/
|
95
|
-
- spec/
|
96
|
-
- spec/
|
97
|
-
- spec/
|
98
|
-
- spec/
|
99
|
-
- spec/lib/basic_assumption/default_assumption/restful_rails_spec.rb
|
81
|
+
- spec/basic_assumption_spec.rb
|
82
|
+
- spec/basic_assumption/configuration_spec.rb
|
83
|
+
- spec/basic_assumption/default_assumption_spec.rb
|
84
|
+
- spec/basic_assumption/default_assumption/base_spec.rb
|
85
|
+
- spec/basic_assumption/default_assumption/class_resolver_spec.rb
|
86
|
+
- spec/basic_assumption/default_assumption/rails_spec.rb
|
87
|
+
- spec/basic_assumption/default_assumption/restful_rails_spec.rb
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'basic_assumption/default_assumption/rails'
|
2
|
-
|
3
|
-
module BasicAssumption
|
4
|
-
module DefaultAssumption
|
5
|
-
# Custom default behavior in the context of Rails.
|
6
|
-
class CautiousRails < BasicAssumption::DefaultAssumption::Rails
|
7
|
-
# Returns a block that will attempt to find an instance of
|
8
|
-
# an ActiveRecord model based on the name that was given to
|
9
|
-
# BasicAssumption#assume and an id value in the parameters.
|
10
|
-
# It will not find based on params[:id], only params[:model_id].
|
11
|
-
# The following two examples would be equivalent:
|
12
|
-
#
|
13
|
-
# class WidgetController < ActionController::Base
|
14
|
-
# assume :widget
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# class WidgetController < ActionController::Base
|
18
|
-
# assume(:widget) { Widget.find(params[:widget_id]) }
|
19
|
-
# end
|
20
|
-
#
|
21
|
-
# It is possible to specify an alternative model name:
|
22
|
-
#
|
23
|
-
# class WidgetController < ApplicationController
|
24
|
-
# assume :sprocket, :as => :widget
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# This will create a +sprocket+ method in your actions and view
|
28
|
-
# that will use the Widget model for its lookup.
|
29
|
-
def block
|
30
|
-
super
|
31
|
-
end
|
32
|
-
|
33
|
-
protected
|
34
|
-
def lookup_id #:nodoc:
|
35
|
-
params["#{name}_id"]
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe BasicAssumption::Configuration do
|
4
|
-
it "provides a #configure class method that yields an instance of the class" do
|
5
|
-
configuration_instance = nil
|
6
|
-
BasicAssumption::Configuration.configure { |instance| configuration_instance = instance }
|
7
|
-
configuration_instance.should be_a_kind_of(BasicAssumption::Configuration)
|
8
|
-
end
|
9
|
-
|
10
|
-
context "an instance" do
|
11
|
-
class Assumer
|
12
|
-
extend BasicAssumption
|
13
|
-
end
|
14
|
-
let(:config) { BasicAssumption::Configuration.new }
|
15
|
-
let(:assuming_class) { Class.new Assumer }
|
16
|
-
it "allows decent_exposure emulation mode" do
|
17
|
-
config.emulate_exposure!
|
18
|
-
assuming_class.should respond_to(:expose)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "can set the default assumption" do
|
22
|
-
config.default_assumption = Proc.new { :qux }
|
23
|
-
assuming_class.class_eval do
|
24
|
-
assume :baz
|
25
|
-
end
|
26
|
-
assuming_class.new.baz.should eql(:qux)
|
27
|
-
end
|
28
|
-
|
29
|
-
after(:all) do
|
30
|
-
config.default_assumption = nil
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'active_support'
|
3
|
-
require 'basic_assumption/default_assumption/cautious_rails'
|
4
|
-
|
5
|
-
class Model; end
|
6
|
-
|
7
|
-
describe BasicAssumption::DefaultAssumption::CautiousRails do
|
8
|
-
|
9
|
-
context "#block" do
|
10
|
-
let(:default) { BasicAssumption::DefaultAssumption::CautiousRails.new }
|
11
|
-
let(:params) { stub(:[] => 42) }
|
12
|
-
|
13
|
-
before(:each) do
|
14
|
-
Model.stub!(:find)
|
15
|
-
default.stub!(:params).and_return(params)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "looks for a params[model_id] in its calling context" do
|
19
|
-
params.should_receive(:[]).with('model_id').and_return(nil)
|
20
|
-
default.block.call(:model, {})
|
21
|
-
end
|
22
|
-
|
23
|
-
it "does not look for params[id] in its calling context" do
|
24
|
-
params.should_receive(:[]).with('id').never
|
25
|
-
default.block.call(:model, {})
|
26
|
-
end
|
27
|
-
|
28
|
-
it "attempts to find a model instance based off the given name" do
|
29
|
-
Model.should_receive(:find).with(42).and_return(:model)
|
30
|
-
default.block.call(:model, {}).should eql(:model)
|
31
|
-
end
|
32
|
-
|
33
|
-
context "when passed an alternative model name" do
|
34
|
-
it "finds a model instance based off the alternative name" do
|
35
|
-
Model.should_receive(:find).with(42).and_return(:model)
|
36
|
-
default.block.call(:my_model, {:as => :model}).should eql(:model)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|