2009-06-17 19:14:05 -04:00
|
|
|
require "active_model"
|
|
|
|
|
2007-07-17 05:44:03 -04:00
|
|
|
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
|
|
|
|
2009-08-01 10:47:44 -04:00
|
|
|
undef_method :to_json
|
|
|
|
|
2009-08-13 04:27:41 -04:00
|
|
|
def to_xml(options={})
|
|
|
|
if options[:builder]
|
|
|
|
options[:builder].name name
|
|
|
|
else
|
|
|
|
"<name>#{name}</name>"
|
|
|
|
end
|
2009-08-01 10:47:44 -04:00
|
|
|
end
|
|
|
|
|
2009-08-13 04:27:41 -04:00
|
|
|
def to_js(options={})
|
|
|
|
"name: #{name.inspect}"
|
2009-08-01 10:47:44 -04:00
|
|
|
end
|
2010-02-16 17:26:29 -05:00
|
|
|
alias :to_text :to_js
|
2009-08-01 10:47:44 -04:00
|
|
|
|
|
|
|
def errors
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?
|
|
|
|
id.present?
|
2009-08-01 10:47:44 -04:00
|
|
|
end
|
2007-07-17 05:44:03 -04:00
|
|
|
end
|
2008-03-31 20:50:09 -04:00
|
|
|
|
|
|
|
class BadCustomer < Customer
|
|
|
|
end
|
|
|
|
|
|
|
|
class GoodCustomer < Customer
|
|
|
|
end
|
2009-03-07 15:05:18 -05:00
|
|
|
|
2012-02-03 11:47:47 -05:00
|
|
|
class ValidatedCustomer < Customer
|
|
|
|
def errors
|
|
|
|
if name =~ /Sikachu/i
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
[{:name => "is invalid"}]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-07 15:05:18 -05:00
|
|
|
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
|
|
|
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?
|
|
|
|
id.present?
|
2009-03-07 15:05:18 -05:00
|
|
|
end
|
|
|
|
end
|
2009-08-01 10:47:44 -04:00
|
|
|
|
|
|
|
class Store < Question
|
|
|
|
end
|
2009-03-07 15:05:18 -05:00
|
|
|
end
|
2009-08-01 10:47:44 -04:00
|
|
|
|
2011-09-09 17:48:43 -04:00
|
|
|
class Post < Struct.new(:title, :author_name, :body, :secret, :persisted, :written_on, :cost)
|
2009-10-03 23:14:50 -04:00
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
2009-12-29 07:06:19 -05:00
|
|
|
extend ActiveModel::Translation
|
2009-10-03 23:14:50 -04:00
|
|
|
|
|
|
|
alias_method :secret?, :secret
|
2011-09-09 17:48:43 -04:00
|
|
|
alias_method :persisted?, :persisted
|
2009-10-03 23:14:50 -04:00
|
|
|
|
2010-10-04 22:32:49 -04:00
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
@persisted = false
|
|
|
|
end
|
|
|
|
|
2009-10-03 23:14:50 -04:00
|
|
|
attr_accessor :author
|
|
|
|
def author_attributes=(attributes); end
|
|
|
|
|
2010-01-13 18:56:29 -05:00
|
|
|
attr_accessor :comments, :comment_ids
|
2009-10-03 23:14:50 -04:00
|
|
|
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
|
2010-02-20 21:05:28 -05:00
|
|
|
def to_key; id ? [id] : nil end
|
2009-10-03 23:14:50 -04:00
|
|
|
def save; @id = 1; @post_id = 1 end
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?; @id.present? end
|
2010-07-24 11:43:50 -04:00
|
|
|
def to_param; @id.to_s; end
|
2009-10-03 23:14:50 -04:00
|
|
|
def name
|
|
|
|
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :relevances
|
|
|
|
def relevances_attributes=(attributes); end
|
|
|
|
|
2010-09-25 10:49:46 -04:00
|
|
|
attr_accessor :body
|
2009-10-03 23:14:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Tag
|
|
|
|
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
|
2010-02-20 21:05:28 -05:00
|
|
|
def to_key; id ? [id] : nil end
|
2009-10-03 23:14:50 -04:00
|
|
|
def save; @id = 1; @post_id = 1 end
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?; @id.present? end
|
2009-10-03 23:14:50 -04:00
|
|
|
def to_param; @id; end
|
|
|
|
def value
|
|
|
|
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :relevances
|
|
|
|
def relevances_attributes=(attributes); end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class CommentRelevance
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
attr_reader :id
|
|
|
|
attr_reader :comment_id
|
|
|
|
def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end
|
2010-02-20 21:05:28 -05:00
|
|
|
def to_key; id ? [id] : nil end
|
2009-10-03 23:14:50 -04:00
|
|
|
def save; @id = 1; @comment_id = 1 end
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?; @id.present? end
|
2009-10-03 23:14:50 -04:00
|
|
|
def to_param; @id; end
|
|
|
|
def value
|
|
|
|
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-03 17:11:34 -04:00
|
|
|
class Sheep
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
attr_reader :id
|
|
|
|
def to_key; id ? [id] : nil end
|
|
|
|
def save; @id = 1 end
|
|
|
|
def new_record?; @id.nil? end
|
|
|
|
def name
|
|
|
|
@id.nil? ? 'new sheep' : "sheep ##{@id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-10-03 23:14:50 -04:00
|
|
|
class TagRelevance
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
attr_reader :id
|
|
|
|
attr_reader :tag_id
|
|
|
|
def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end
|
2010-02-20 21:05:28 -05:00
|
|
|
def to_key; id ? [id] : nil end
|
2009-10-03 23:14:50 -04:00
|
|
|
def save; @id = 1; @tag_id = 1 end
|
2010-02-21 05:09:21 -05:00
|
|
|
def persisted?; @id.present? end
|
2009-10-03 23:14:50 -04:00
|
|
|
def to_param; @id; end
|
|
|
|
def value
|
|
|
|
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Author < Comment
|
|
|
|
attr_accessor :post
|
|
|
|
def post_attributes=(attributes); end
|
|
|
|
end
|
2010-08-31 19:37:02 -04:00
|
|
|
|
2011-06-21 21:24:45 -04:00
|
|
|
class HashBackedAuthor < Hash
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
def persisted?; false; end
|
|
|
|
|
|
|
|
def name
|
|
|
|
"hash backed author"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-31 19:37:02 -04:00
|
|
|
module Blog
|
2011-11-23 14:06:45 -05:00
|
|
|
def self.use_relative_model_naming?
|
|
|
|
true
|
2010-08-31 19:37:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Post < Struct.new(:title, :id)
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
def persisted?
|
|
|
|
id.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-11-11 10:02:14 -05:00
|
|
|
|
|
|
|
class ArelLike
|
|
|
|
def to_ary
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def each
|
|
|
|
a = Array.new(2) { |id| Comment.new(id + 1) }
|
|
|
|
a.each { |i| yield i }
|
|
|
|
end
|
|
|
|
end
|
2010-11-15 17:52:39 -05:00
|
|
|
|
|
|
|
class RenderJsonTestException < Exception
|
|
|
|
def to_json(options = nil)
|
2010-11-28 06:48:50 -05:00
|
|
|
return { :error => self.class.name, :message => self.to_s }.to_json
|
2010-11-15 17:52:39 -05:00
|
|
|
end
|
|
|
|
end
|
2012-05-16 18:20:32 -04:00
|
|
|
|
|
|
|
class Car < Struct.new(:color)
|
|
|
|
end
|