Skip to content

Commit 166dbaa

Browse files
committed
Remove ActiveModel dependency from ActionPack
ActiveModel is used in ActionPack for ActiveModel::Naming for a few, mostly optional aspects of ActionPack related to automatically converting an ActiveModel compliant object into a key for params and routing. It uses only three methods of ActiveModel (ActiveModel::Naming.route_key, ActiveModel::Naming.singular_route_key and ActiveModel::Naming.param_key).
1 parent 809bdf3 commit 166dbaa

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

actionpack/actionpack.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ Gem::Specification.new do |s|
1818
s.requirements << 'none'
1919

2020
s.add_dependency('activesupport', version)
21-
s.add_dependency('activemodel', version)
2221
s.add_dependency('rack-cache', '~> 1.2')
2322
s.add_dependency('builder', '~> 3.0.0')
2423
s.add_dependency('rack', '~> 1.4.1')
2524
s.add_dependency('rack-test', '~> 0.6.1')
2625
s.add_dependency('journey', '~> 1.0.1')
2726
s.add_dependency('erubis', '~> 2.7.0')
2827

28+
s.add_development_dependency('activemodel', version)
2929
s.add_development_dependency('tzinfo', '~> 0.3.33')
3030
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ActionController
2+
module ModelNaming
3+
# Converts the given object to an ActiveModel compliant one.
4+
def convert_to_model(object)
5+
object.respond_to?(:to_model) ? object.to_model : object
6+
end
7+
8+
def model_name_from_record_or_class(record_or_class)
9+
(record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
10+
end
11+
end
12+
end

actionpack/lib/action_controller/record_identifier.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'active_support/core_ext/module'
2+
require 'action_controller/model_naming'
23

34
module ActionController
45
# The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
@@ -27,6 +28,8 @@ module ActionController
2728
module RecordIdentifier
2829
extend self
2930

31+
include ModelNaming
32+
3033
JOIN = '_'.freeze
3134
NEW = 'new'.freeze
3235

@@ -40,7 +43,7 @@ module RecordIdentifier
4043
# dom_class(post, :edit) # => "edit_post"
4144
# dom_class(Person, :edit) # => "edit_person"
4245
def dom_class(record_or_class, prefix = nil)
43-
singular = ActiveModel::Naming.param_key(record_or_class)
46+
singular = model_name_from_record_or_class(record_or_class).param_key
4447
prefix ? "#{prefix}#{JOIN}#{singular}" : singular
4548
end
4649

@@ -73,8 +76,7 @@ def dom_id(record, prefix = nil)
7376
# method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
7477
# make sure yourself that your dom ids are valid, in case you overwrite this method.
7578
def record_key_for_dom_id(record)
76-
record = record.to_model if record.respond_to?(:to_model)
77-
key = record.to_key
79+
key = convert_to_model(record).to_key
7880
key ? key.join('_') : key
7981
end
8082
end

actionpack/lib/action_dispatch/routing/polymorphic_routes.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'action_controller/model_naming'
2+
13
module ActionDispatch
24
module Routing
35
# Polymorphic URL helpers are methods for smart resolution to a named route call when
@@ -53,6 +55,8 @@ module Routing
5355
# form_for([blog, @post]) # => "/blog/posts/1"
5456
#
5557
module PolymorphicRoutes
58+
include ActionController::ModelNaming
59+
5660
# Constructs a call to a named RESTful route for the given record and returns the
5761
# resulting URL string. For example:
5862
#
@@ -154,10 +158,6 @@ def action_prefix(options)
154158
options[:action] ? "#{options[:action]}_" : ''
155159
end
156160

157-
def convert_to_model(object)
158-
object.respond_to?(:to_model) ? object.to_model : object
159-
end
160-
161161
def routing_type(options)
162162
options[:routing_type] || :url
163163
end
@@ -169,7 +169,7 @@ def build_named_route_call(records, inflection, options = {})
169169
if parent.is_a?(Symbol) || parent.is_a?(String)
170170
parent
171171
else
172-
ActiveModel::Naming.singular_route_key(parent)
172+
model_name_from_record_or_class(parent).singular_route_key
173173
end
174174
end
175175
else
@@ -181,9 +181,9 @@ def build_named_route_call(records, inflection, options = {})
181181
route << record
182182
elsif record
183183
if inflection == :singular
184-
route << ActiveModel::Naming.singular_route_key(record)
184+
route << model_name_from_record_or_class(record).singular_route_key
185185
else
186-
route << ActiveModel::Naming.route_key(record)
186+
route << model_name_from_record_or_class(record).route_key
187187
end
188188
else
189189
raise ArgumentError, "Nil location provided. Can't build URI."

actionpack/lib/action_view/helpers/form_helper.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'active_support/core_ext/array/extract_options'
1313
require 'active_support/deprecation'
1414
require 'active_support/core_ext/string/inflections'
15+
require 'action_controller/model_naming'
1516

1617
module ActionView
1718
# = Action View Form Helpers
@@ -117,11 +118,7 @@ module FormHelper
117118

118119
include FormTagHelper
119120
include UrlHelper
120-
121-
# Converts the given object to an ActiveModel compliant one.
122-
def convert_to_model(object)
123-
object.respond_to?(:to_model) ? object.to_model : object
124-
end
121+
include ActionController::ModelNaming
125122

126123
# Creates a form that allows the user to create or update the attributes
127124
# of a specific model object.
@@ -411,7 +408,7 @@ def form_for(record, options = {}, &proc)
411408
object = nil
412409
else
413410
object = record.is_a?(Array) ? record.last : record
414-
object_name = options[:as] || ActiveModel::Naming.param_key(object)
411+
object_name = options[:as] || model_name_from_record_or_class(object).param_key
415412
apply_form_for_options!(record, object, options)
416413
end
417414

@@ -1128,7 +1125,7 @@ def instantiate_builder(record_name, record_object, options)
11281125
object_name = record_name
11291126
else
11301127
object = record_name
1131-
object_name = ActiveModel::Naming.param_key(object)
1128+
object_name = model_name_from_record_or_class(object).param_key
11321129
end
11331130

11341131
builder = options[:builder] || default_form_builder
@@ -1142,9 +1139,11 @@ def default_form_builder
11421139
end
11431140

11441141
class FormBuilder
1142+
include ActionController::ModelNaming
1143+
11451144
# The methods which wrap a form helper call.
11461145
class_attribute :field_helpers
1147-
self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model]
1146+
self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model, :model_name_from_record_or_class]
11481147

11491148
attr_accessor :object_name, :object, :options
11501149

@@ -1214,7 +1213,7 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
12141213
end
12151214
else
12161215
record_object = record_name.is_a?(Array) ? record_name.last : record_name
1217-
record_name = ActiveModel::Naming.param_key(record_object)
1216+
record_name = model_name_from_record_or_class(record_object).param_key
12181217
end
12191218

12201219
index = if options.has_key?(:index)
@@ -1396,10 +1395,6 @@ def nested_child_index(name)
13961395
@nested_child_index[name] ||= -1
13971396
@nested_child_index[name] += 1
13981397
end
1399-
1400-
def convert_to_model(object)
1401-
object.respond_to?(:to_model) ? object.to_model : object
1402-
end
14031398
end
14041399
end
14051400

0 commit comments

Comments
 (0)