From 166dbaa7526a96fdf046f093f25b0a134b277a68 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Thu, 28 Jun 2012 23:54:12 -0500 Subject: 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). --- actionpack/actionpack.gemspec | 2 +- actionpack/lib/action_controller/model_naming.rb | 12 ++++++++++++ .../lib/action_controller/record_identifier.rb | 8 +++++--- .../action_dispatch/routing/polymorphic_routes.rb | 14 +++++++------- actionpack/lib/action_view/helpers/form_helper.rb | 21 ++++++++------------- 5 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 actionpack/lib/action_controller/model_naming.rb (limited to 'actionpack') diff --git a/actionpack/actionpack.gemspec b/actionpack/actionpack.gemspec index 938f3d6dc2..6075e2f02b 100644 --- a/actionpack/actionpack.gemspec +++ b/actionpack/actionpack.gemspec @@ -18,7 +18,6 @@ Gem::Specification.new do |s| s.requirements << 'none' s.add_dependency('activesupport', version) - s.add_dependency('activemodel', version) s.add_dependency('rack-cache', '~> 1.2') s.add_dependency('builder', '~> 3.0.0') s.add_dependency('rack', '~> 1.4.1') @@ -26,5 +25,6 @@ Gem::Specification.new do |s| s.add_dependency('journey', '~> 1.0.1') s.add_dependency('erubis', '~> 2.7.0') + s.add_development_dependency('activemodel', version) s.add_development_dependency('tzinfo', '~> 0.3.33') end diff --git a/actionpack/lib/action_controller/model_naming.rb b/actionpack/lib/action_controller/model_naming.rb new file mode 100644 index 0000000000..785221dc3d --- /dev/null +++ b/actionpack/lib/action_controller/model_naming.rb @@ -0,0 +1,12 @@ +module ActionController + module ModelNaming + # Converts the given object to an ActiveModel compliant one. + def convert_to_model(object) + object.respond_to?(:to_model) ? object.to_model : object + end + + def model_name_from_record_or_class(record_or_class) + (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name + end + end +end diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 16a5decc62..d3ac406618 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/module' +require 'action_controller/model_naming' module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or @@ -27,6 +28,8 @@ module ActionController module RecordIdentifier extend self + include ModelNaming + JOIN = '_'.freeze NEW = 'new'.freeze @@ -40,7 +43,7 @@ module ActionController # dom_class(post, :edit) # => "edit_post" # dom_class(Person, :edit) # => "edit_person" def dom_class(record_or_class, prefix = nil) - singular = ActiveModel::Naming.param_key(record_or_class) + singular = model_name_from_record_or_class(record_or_class).param_key prefix ? "#{prefix}#{JOIN}#{singular}" : singular end @@ -73,8 +76,7 @@ module ActionController # method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to # make sure yourself that your dom ids are valid, in case you overwrite this method. def record_key_for_dom_id(record) - record = record.to_model if record.respond_to?(:to_model) - key = record.to_key + key = convert_to_model(record).to_key key ? key.join('_') : key end end diff --git a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb index 86ce7a83b9..3d7b8878b8 100644 --- a/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb +++ b/actionpack/lib/action_dispatch/routing/polymorphic_routes.rb @@ -1,3 +1,5 @@ +require 'action_controller/model_naming' + module ActionDispatch module Routing # Polymorphic URL helpers are methods for smart resolution to a named route call when @@ -53,6 +55,8 @@ module ActionDispatch # form_for([blog, @post]) # => "/blog/posts/1" # module PolymorphicRoutes + include ActionController::ModelNaming + # Constructs a call to a named RESTful route for the given record and returns the # resulting URL string. For example: # @@ -154,10 +158,6 @@ module ActionDispatch options[:action] ? "#{options[:action]}_" : '' end - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end - def routing_type(options) options[:routing_type] || :url end @@ -169,7 +169,7 @@ module ActionDispatch if parent.is_a?(Symbol) || parent.is_a?(String) parent else - ActiveModel::Naming.singular_route_key(parent) + model_name_from_record_or_class(parent).singular_route_key end end else @@ -181,9 +181,9 @@ module ActionDispatch route << record elsif record if inflection == :singular - route << ActiveModel::Naming.singular_route_key(record) + route << model_name_from_record_or_class(record).singular_route_key else - route << ActiveModel::Naming.route_key(record) + route << model_name_from_record_or_class(record).route_key end else raise ArgumentError, "Nil location provided. Can't build URI." diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index ba2a26fd09..b34f6c8650 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -12,6 +12,7 @@ require 'active_support/core_ext/string/output_safety' require 'active_support/core_ext/array/extract_options' require 'active_support/deprecation' require 'active_support/core_ext/string/inflections' +require 'action_controller/model_naming' module ActionView # = Action View Form Helpers @@ -117,11 +118,7 @@ module ActionView include FormTagHelper include UrlHelper - - # Converts the given object to an ActiveModel compliant one. - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end + include ActionController::ModelNaming # Creates a form that allows the user to create or update the attributes # of a specific model object. @@ -411,7 +408,7 @@ module ActionView object = nil else object = record.is_a?(Array) ? record.last : record - object_name = options[:as] || ActiveModel::Naming.param_key(object) + object_name = options[:as] || model_name_from_record_or_class(object).param_key apply_form_for_options!(record, object, options) end @@ -1128,7 +1125,7 @@ module ActionView object_name = record_name else object = record_name - object_name = ActiveModel::Naming.param_key(object) + object_name = model_name_from_record_or_class(object).param_key end builder = options[:builder] || default_form_builder @@ -1142,9 +1139,11 @@ module ActionView end class FormBuilder + include ActionController::ModelNaming + # The methods which wrap a form helper call. class_attribute :field_helpers - self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model] + self.field_helpers = FormHelper.instance_methods - [:form_for, :convert_to_model, :model_name_from_record_or_class] attr_accessor :object_name, :object, :options @@ -1214,7 +1213,7 @@ module ActionView end else record_object = record_name.is_a?(Array) ? record_name.last : record_name - record_name = ActiveModel::Naming.param_key(record_object) + record_name = model_name_from_record_or_class(record_object).param_key end index = if options.has_key?(:index) @@ -1396,10 +1395,6 @@ module ActionView @nested_child_index[name] ||= -1 @nested_child_index[name] += 1 end - - def convert_to_model(object) - object.respond_to?(:to_model) ? object.to_model : object - end end end -- cgit v1.2.3