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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
2010-11-15 17:52:39 -05:00
|
|
|
class RenderJsonTestException < Exception
|
2013-11-05 23:05:58 -05:00
|
|
|
def as_json(options = nil)
|
|
|
|
{ :error => self.class.name, :message => self.to_s }
|
2010-11-15 17:52:39 -05:00
|
|
|
end
|
|
|
|
end
|