-
Notifications
You must be signed in to change notification settings - Fork 3
/
rails.rb
181 lines (164 loc) · 5.47 KB
/
rails.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require 'basic_assumption/default_assumption/action'
require 'basic_assumption/default_assumption/class_resolver'
require 'basic_assumption/default_assumption/name'
require 'basic_assumption/default_assumption/owner_attributes'
module BasicAssumption
module DefaultAssumption
# Restful default behavior in the context of Rails
class Rails
attr_reader :action, :context, :name, :request #:nodoc:
def initialize(name=nil, context={}, request=nil) #:nodoc:
@context = context
@name = Name.new(context.delete(:as) || name)
@request = request
@action = initialize_action
end
# Returns a block that will attempt to do the correct thing depending
# on the plurality of the name passed to +assume+ and the action for the
# current request. If the name is singular and the action is not 'new'
# or 'create', then +assume+ will find an instance of
# an ActiveRecord model of the name that it received and an id
# value in the parameters. If the action is 'new' or 'create', +assume+
# will instantiate a new instance of the model class, passing in the
# values it finds in the +params+ hash with for a key of the name passed
# to +assume+. For example:
#
# class WidgetController < ApplicationController
# default_assumption :rails
# assume :widget
#
# def create
# widget.save! # widget is: Widget.new(params[:widget])
# end
# end
#
# Note the object will have been instantiated but not saved, destroyed,
# etc.
#
# If the name passed to assume is plural, +assume+ returns all records
# for the model.
#
# It is possible to specify an alternative model name:
#
# class WidgetController < ApplicationController
# assume :sprocket, :as => :widget
# end
#
# This will create a +sprocket+ method in your actions and view
# that will use the Widget model for its lookup.
#
# The following two examples would be equivalent:
#
# class WidgetController < ActionController::Base
# assume :widget
# end
#
# class WidgetController < ActionController::Base
# assume(:widget) { Widget.find(params[:widget_id]) rescue nil }
# end
#
# The find can also fall back to using params[:id] when
# :find_on_id is specified. The following are equivalent:
#
# class WidgetController < ActionController::Base
# assume :widget, :find_on_id => true
# end
#
# class WidgetController < ActionController::Base
# assume(:widget) { Widget.find(params[:widget_id] || params[:id]) rescue nil }
# end
#
# The find will, by default, swallow errors encountered
# when finding. This can be overridden by setting :raise_error.
#
# class WidgetController < ActionController::Base
# assume :widget, :raise_error => true
# end
#
# class WidgetController < ActionController::Base
# assume(:widget) { Widget.find(params[:widget_id]) }
# end
#
# Both of these settings can be turned on by default via
# configuration options, such as:
#
# conf.active_record.raise_error = true
# conf.active_record.find_on_id = true
#
# It is possible to specify an alternative model name:
#
# class WidgetController < ApplicationController
# assume :sprocket, :as => :widget
# end
#
# This will create a +sprocket+ method in your actions and view
# that will use the Widget model for its lookup.
def block
default = self
Proc.new do |name, context|
context[:controller] = self
default.class.new(name, context, request).result
end
end
def result #:nodoc:
action.outcome
end
protected
def initialize_action
name.plural? ? plural_action : singular_action
end
def singular_action
Action.new(request) do |action|
action.find do
find_and_maybe_raise
end
action.update do
record = find_and_maybe_raise
record.attributes = resource_attributes
record
end
action.default do
model_class.new(resource_attributes.merge(owner_attributes))
end
end
end
def plural_action
Action.new(request) do |action|
action.default do
finder_scope
end
end
end
def finder_scope
model_class.where(owner_attributes)
end
def model_class
@model_class ||= ClassResolver.new(name).klass
end
def params
@params ||= request ? request.params : {}
end
def find_and_maybe_raise
begin
finder_scope.find(params['id'])
rescue
raise if settings[:raise_error]
end
end
def resource_attributes
params[name.singular] || {}
end
def owner_attributes
@owner_attributes ||= if context[:owner]
OwnerAttributes.new(context[:owner], context[:controller]).attributes
else
{}
end
end
def settings #:nodoc:
@global_settings ||= BasicAssumption::Configuration.settings
@global_settings.merge(context)
end
end
end
end