1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/test/lib/controller/fake_models.rb

119 lines
2.2 KiB
Ruby
Raw Normal View History

2009-06-17 19:14:05 -04:00
require "active_model"
class Customer < Struct.new(:name, :id)
2009-07-21 01:51:57 -04:00
extend ActiveModel::Naming
include ActiveModel::Conversion
2009-06-17 11:37:39 -04:00
undef_method :to_json
def to_xml(options={})
if options[:builder]
options[:builder].name name
else
"<name>#{name}</name>"
end
end
def to_js(options={})
"name: #{name.inspect}"
end
alias :to_text :to_js
def errors
[]
end
def persisted?
id.present?
end
end
class ValidatedCustomer < Customer
def errors
if name =~ /Sikachu/i
[]
else
[{:name => "is invalid"}]
end
end
end
module Quiz
class Question < Struct.new(:name, :id)
2009-07-21 01:51:57 -04:00
extend ActiveModel::Naming
include ActiveModel::Conversion
2009-06-17 11:37:39 -04:00
def persisted?
id.present?
end
end
class Store < Question
end
end
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
extend ActiveModel::Naming
include ActiveModel::Conversion
extend ActiveModel::Translation
alias_method :secret?, :secret
alias_method :persisted?, :persisted
2010-10-04 22:32:49 -04:00
def initialize(*args)
super
@persisted = false
end
attr_accessor :author
def author_attributes=(attributes); end
attr_accessor :comments, :comment_ids
def comments_attributes=(attributes); end
attr_accessor :tags
def tags_attributes=(attributes); end
end
class Comment
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
attr_reader :post_id
def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
AMo #key is now #to_key and CI is probably happy Obviously #key is a too common name to be included in the AMo interface, #to_key fits better and also relates nicely to #to_param. Thx wycats, koz and josevalim for the suggestion. AR's #to_key implementation now takes customized primary keys into account and there's a testcase for that too. The #to_param AMo lint makes no assumptions on how the method behaves in the presence of composite primary keys. It leaves the decision wether to provide a default, or to raise and thus signal to the user that implementing this method will need his special attention, up to the implementers. All AMo cares about is that #to_param is implemented and returns nil in case of a new_record?. The default CompliantObject used in lint_test provides a naive default implementation that just joins all key attributes with '-'. The #to_key default implementation in lint_test's CompliantObject now returns [id] instead of [1]. This was previously causing the (wrong) tests I added for AR's #to_key implementation to pass. The #to_key tests added with this patch should be better. The CI failure was caused by my lack of knowledge about the test:isolated task. The tests for the record_identifier code in action_controller are using fake non AR models and I forgot to stub the #to_key method over there. This issue didn't come up when running the test task, only test:isolated revealed it. This patch fixes that. All tests pass isolated or not, well, apart from one previously unpended test in action_controller that is unrelated to my patch.
2010-02-20 21:05:28 -05:00
def to_key; id ? [id] : nil end
def save; @id = 1; @post_id = 1 end
def persisted?; @id.present? end
2010-07-24 11:43:50 -04:00
def to_param; @id.to_s; end
def name
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
end
attr_accessor :relevances
def relevances_attributes=(attributes); end
attr_accessor :body
end
module Blog
def self.use_relative_model_naming?
true
end
class Post < Struct.new(:title, :id)
extend ActiveModel::Naming
include ActiveModel::Conversion
def persisted?
id.present?
end
end
end
class RenderJsonTestException < Exception
def as_json(options = nil)
{ :error => self.class.name, :message => self.to_s }
end
end