Skip to content

Commit

Permalink
Remove ActiveModel dependency from ActionPack
Browse files Browse the repository at this point in the history
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
guilleiguaran committed Jun 30, 2012
1 parent 809bdf3 commit 166dbaa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion actionpack/actionpack.gemspec
Expand Up @@ -18,13 +18,13 @@ 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')
s.add_dependency('rack-test', '~> 0.6.1')
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
12 changes: 12 additions & 0 deletions 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
8 changes: 5 additions & 3 deletions 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
Expand Down Expand Up @@ -27,6 +28,8 @@ module ActionController
module RecordIdentifier
extend self

include ModelNaming

JOIN = '_'.freeze
NEW = 'new'.freeze

Expand All @@ -40,7 +43,7 @@ module RecordIdentifier
# 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

Expand Down Expand Up @@ -73,8 +76,7 @@ def dom_id(record, prefix = nil)
# 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
Expand Down
14 changes: 7 additions & 7 deletions 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
Expand Down Expand Up @@ -53,6 +55,8 @@ module Routing
# 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:
#
Expand Down Expand Up @@ -154,10 +158,6 @@ def action_prefix(options)
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
Expand All @@ -169,7 +169,7 @@ def build_named_route_call(records, inflection, options = {})
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
Expand All @@ -181,9 +181,9 @@ def build_named_route_call(records, inflection, options = {})
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."
Expand Down
21 changes: 8 additions & 13 deletions actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -12,6 +12,7 @@
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
Expand Down Expand Up @@ -117,11 +118,7 @@ module FormHelper

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.
Expand Down Expand Up @@ -411,7 +408,7 @@ def form_for(record, options = {}, &proc)
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

Expand Down Expand Up @@ -1128,7 +1125,7 @@ def instantiate_builder(record_name, record_object, options)
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
Expand All @@ -1142,9 +1139,11 @@ def default_form_builder
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

Expand Down Expand Up @@ -1214,7 +1213,7 @@ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
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)
Expand Down Expand Up @@ -1396,10 +1395,6 @@ def nested_child_index(name)
@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

Expand Down

0 comments on commit 166dbaa

Please sign in to comment.