mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #6904 from guilleiguaran/remove-amo-dependency-in-ap
Remove Active Model dependency from Action Pack
This commit is contained in:
commit
49d069d797
5 changed files with 33 additions and 24 deletions
|
@ -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
|
||||
|
|
12
actionpack/lib/action_controller/model_naming.rb
Normal file
12
actionpack/lib/action_controller/model_naming.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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."
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue