Skip to content
This repository
Browse code

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).
  • Loading branch information...
commit 166dbaa7526a96fdf046f093f25b0a134b277a68 1 parent 809bdf3
Guillermo Iguaran authored June 28, 2012
2  actionpack/actionpack.gemspec
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
18 18
   s.requirements << 'none'
19 19
 
20 20
   s.add_dependency('activesupport', version)
21  
-  s.add_dependency('activemodel',   version)
22 21
   s.add_dependency('rack-cache',    '~> 1.2')
23 22
   s.add_dependency('builder',       '~> 3.0.0')
24 23
   s.add_dependency('rack',          '~> 1.4.1')
@@ -26,5 +25,6 @@ Gem::Specification.new do |s|
26 25
   s.add_dependency('journey',       '~> 1.0.1')
27 26
   s.add_dependency('erubis',        '~> 2.7.0')
28 27
 
  28
+  s.add_development_dependency('activemodel', version)
29 29
   s.add_development_dependency('tzinfo', '~> 0.3.33')
30 30
 end
12  actionpack/lib/action_controller/model_naming.rb
... ...
@@ -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
8  actionpack/lib/action_controller/record_identifier.rb
... ...
@@ -1,4 +1,5 @@
1 1
 require 'active_support/core_ext/module'
  2
+require 'action_controller/model_naming'
2 3
 
3 4
 module ActionController
4 5
   # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or
@@ -27,6 +28,8 @@ module ActionController
27 28
   module RecordIdentifier
28 29
     extend self
29 30
 
  31
+    include ModelNaming
  32
+
30 33
     JOIN = '_'.freeze
31 34
     NEW = 'new'.freeze
32 35
 
@@ -40,7 +43,7 @@ module RecordIdentifier
40 43
     #   dom_class(post, :edit)   # => "edit_post"
41 44
     #   dom_class(Person, :edit) # => "edit_person"
42 45
     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
44 47
       prefix ? "#{prefix}#{JOIN}#{singular}" : singular
45 48
     end
46 49
 
@@ -73,8 +76,7 @@ def dom_id(record, prefix = nil)
73 76
     # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
74 77
     # make sure yourself that your dom ids are valid, in case you overwrite this method.
75 78
     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
78 80
       key ? key.join('_') : key
79 81
     end
80 82
   end
14  actionpack/lib/action_dispatch/routing/polymorphic_routes.rb
... ...
@@ -1,3 +1,5 @@
  1
+require 'action_controller/model_naming'
  2
+
1 3
 module ActionDispatch
2 4
   module Routing
3 5
     # Polymorphic URL helpers are methods for smart resolution to a named route call when
@@ -53,6 +55,8 @@ module Routing
53 55
     #   form_for([blog, @post])         # => "/blog/posts/1"
54 56
     #
55 57
     module PolymorphicRoutes
  58
+      include ActionController::ModelNaming
  59
+
56 60
       # Constructs a call to a named RESTful route for the given record and returns the
57 61
       # resulting URL string. For example:
58 62
       #
@@ -154,10 +158,6 @@ def action_prefix(options)
154 158
           options[:action] ? "#{options[:action]}_" : ''
155 159
         end
156 160
 
157  
-        def convert_to_model(object)
158  
-          object.respond_to?(:to_model) ? object.to_model : object
159  
-        end
160  
-
161 161
         def routing_type(options)
162 162
           options[:routing_type] || :url
163 163
         end
@@ -169,7 +169,7 @@ def build_named_route_call(records, inflection, options = {})
169 169
               if parent.is_a?(Symbol) || parent.is_a?(String)
170 170
                 parent
171 171
               else
172  
-                ActiveModel::Naming.singular_route_key(parent)
  172
+                model_name_from_record_or_class(parent).singular_route_key
173 173
               end
174 174
             end
175 175
           else
@@ -181,9 +181,9 @@ def build_named_route_call(records, inflection, options = {})
181 181
             route << record
182 182
           elsif record
183 183
             if inflection == :singular
184  
-              route << ActiveModel::Naming.singular_route_key(record)
  184
+              route << model_name_from_record_or_class(record).singular_route_key
185 185
             else
186  
-              route << ActiveModel::Naming.route_key(record)
  186
+              route << model_name_from_record_or_class(record).route_key
187 187
             end
188 188
           else
189 189
             raise ArgumentError, "Nil location provided. Can't build URI."
21  actionpack/lib/action_view/helpers/form_helper.rb
@@ -12,6 +12,7 @@
12 12
 require 'active_support/core_ext/array/extract_options'
13 13
 require 'active_support/deprecation'
14 14
 require 'active_support/core_ext/string/inflections'
  15
+require 'action_controller/model_naming'
15 16
 
16 17
 module ActionView
17 18
   # = Action View Form Helpers
@@ -117,11 +118,7 @@ module FormHelper
117 118
 
118 119
       include FormTagHelper
119 120
       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
125 122
 
126 123
       # Creates a form that allows the user to create or update the attributes
127 124
       # of a specific model object.
@@ -411,7 +408,7 @@ def form_for(record, options = {}, &proc)
411 408
           object      = nil
412 409
         else
413 410
           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
415 412
           apply_form_for_options!(record, object, options)
416 413
         end
417 414
 
@@ -1128,7 +1125,7 @@ def instantiate_builder(record_name, record_object, options)
1128 1125
             object_name = record_name
1129 1126
           else
1130 1127
             object = record_name
1131  
-            object_name = ActiveModel::Naming.param_key(object)
  1128
+            object_name = model_name_from_record_or_class(object).param_key
1132 1129
           end
1133 1130
 
1134 1131
           builder = options[:builder] || default_form_builder
@@ -1142,9 +1139,11 @@ def default_form_builder
1142 1139
     end
1143 1140
 
1144 1141
     class FormBuilder
  1142
+      include ActionController::ModelNaming
  1143
+
1145 1144
       # The methods which wrap a form helper call.
1146 1145
       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]
1148 1147
 
1149 1148
       attr_accessor :object_name, :object, :options
1150 1149
 
@@ -1214,7 +1213,7 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
1214 1213
           end
1215 1214
         else
1216 1215
           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
1218 1217
         end
1219 1218
 
1220 1219
         index = if options.has_key?(:index)
@@ -1396,10 +1395,6 @@ def nested_child_index(name)
1396 1395
           @nested_child_index[name] ||= -1
1397 1396
           @nested_child_index[name] += 1
1398 1397
         end
1399  
-
1400  
-        def convert_to_model(object)
1401  
-          object.respond_to?(:to_model) ? object.to_model : object
1402  
-        end
1403 1398
     end
1404 1399
   end
1405 1400
 

0 notes on commit 166dbaa

Please sign in to comment.
Something went wrong with that request. Please try again.