提交 92c00d75 编写于 作者: J Joshua Peek

AMo conversion helper

上级 6944b391
......@@ -126,7 +126,8 @@ def render_with_record_collection
end
class Game < Struct.new(:name, :id)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
def to_param
id.to_s
end
......
require 'abstract_unit'
class Comment
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
def save; @id = 1 end
......
......@@ -4,7 +4,8 @@ class WorkshopsController < ActionController::Base
end
class Workshop
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :id, :new_record
def initialize(id, new_record)
......
require "active_model"
class Customer < Struct.new(:name, :id)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
def to_param
id.to_s
......@@ -16,7 +17,8 @@ class GoodCustomer < Customer
module Quiz
class Question < Struct.new(:name, :id)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
def to_param
id.to_s
......
......@@ -5,15 +5,18 @@ class ActiveRecordHelperTest < ActionView::TestCase
silence_warnings do
class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class User < Struct.new(:email)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class Column < Struct.new(:type, :name, :human_name)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
end
end
......
require 'abstract_unit'
class Scroll < Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
def new_record?
true
......
......@@ -2,7 +2,8 @@
silence_warnings do
class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
alias_method :secret?, :secret
......@@ -25,7 +26,8 @@ def tags_attributes=(attributes); end
end
class Comment
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :post_id
......@@ -43,7 +45,8 @@ def relevances_attributes=(attributes); end
end
class Tag
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :post_id
......@@ -61,7 +64,8 @@ def relevances_attributes=(attributes); end
end
class CommentRelevance
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :comment_id
......@@ -75,7 +79,8 @@ def value
end
class TagRelevance
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :tag_id
......
require 'abstract_unit'
require 'active_model'
Bunny = Struct.new(:Bunny, :id)
Bunny.extend ActiveModel::APICompliant
class Bunny < Struct.new(:Bunny, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class Author
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
def save; @id = 1 end
......@@ -16,7 +19,8 @@ def name
end
class Article
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :author_id
def save; @id = 1; @author_id = 1 end
......
require 'abstract_unit'
class Post
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
def id
45
end
......
......@@ -41,7 +41,7 @@ def test_homepage_url
def test_link_to_person
person = mock(:name => "David")
person.class.extend ActiveModel::APICompliant
person.class.extend ActiveModel::Naming
expects(:mocha_mock_path).with(person).returns("/people/1")
assert_equal '<a href="/people/1">David</a>', link_to_person(person)
end
......
......@@ -494,7 +494,8 @@ def with_restful_routing
end
class Workshop
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :id, :new_record
def initialize(id, new_record)
......@@ -511,7 +512,8 @@ def to_s
end
class Session
extend ActiveModel::APICompliant
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :id, :workshop_id, :new_record
def initialize(id, new_record)
......
......@@ -26,7 +26,7 @@
require 'active_support'
module ActiveModel
autoload :APICompliant, 'active_model/api_compliant'
autoload :Conversion, 'active_model/conversion'
autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods'
autoload :Errors, 'active_model/errors'
autoload :Name, 'active_model/naming'
......
module ActiveModel
module APICompliant
include Naming
def self.extended(klass)
klass.class_eval do
include Validations
include InstanceMethods
end
end
module InstanceMethods
def to_model
if respond_to?(:new_record?)
self.class.class_eval { def to_model() self end }
to_model
else
raise "In order to be ActiveModel API compliant, you need to define " \
"a new_record? method, which should return true if it has not " \
"yet been persisted."
end
end
end
end
end
\ No newline at end of file
module ActiveModel
# Include ActiveModel::Conversion if your object "acts like an ActiveModel model".
module Conversion
def to_model
self
end
end
end
......@@ -2511,13 +2511,6 @@ def to_param
(id = self.id) ? id.to_s : nil # Be sure to stringify the id for routes
end
# Returns the ActiveRecord object when asked for its
# ActiveModel-compliant representation, because ActiveRecord is
# ActiveModel-compliant.
def to_model
self
end
# Returns a cache key that can be used to identify this record.
#
# ==== Examples
......@@ -3186,6 +3179,7 @@ def clone_attribute_value(reader_method, attribute_name)
include Dirty
include Callbacks, ActiveModel::Observing, Timestamp
include Associations, AssociationPreload, NamedScope
include ActiveModel::Conversion
# AutosaveAssociation needs to be included before Transactions, because we want
# #save_with_autosave_associations to be wrapped inside a transaction.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册