Skip to content

Instantly share code, notes, and snippets.

@snusnu
Created September 23, 2009 02:56
Show Gist options
  • Save snusnu/191656 to your computer and use it in GitHub Desktop.
Save snusnu/191656 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
require 'spec'
module DataMapper
module ActiveModel
# This is mostly copied from active_model and adapted to use extlib under the hood.
# see http://github.com/rails/rails/blob/master/activemodel/lib/active_model/naming.rb
class Name < String
attr_reader :singular, :plural, :element, :collection, :partial_path, :human
alias_method :cache_key, :collection
def initialize(name)
super
@singular = Extlib::Inflection.underscore(self).tr('/', '_').freeze
@plural = Extlib::Inflection.pluralize(@singular).freeze
@element = Extlib::Inflection.underscore(Extlib::Inflection.demodulize(self)).freeze
@human = @element.gsub(/_/, " ")
@collection = Extlib::Inflection.tableize(self).freeze
@partial_path = "#{@collection}/#{@element}".freeze
end
end
module ClassMethods
# Returns an ActiveModel::Name object for module. It can be
# used to retrieve all kinds of naming-related information.
def model_name
@_model_name ||= DataMapper::ActiveModel::Name.new(name)
end
end
module InstanceMethods
def self.included(host)
host.class_eval do
alias :new_record? :new?
end
end
def to_model
self
end
# Define the minimum requirements if the resource
# has no concept of validation baked in, which
# happens if dm-validations is not required.
unless respond_to?(:validatable?)
def valid?
true
end
def errors
hash = {}
def hash.full_messages; [] end
hash
end
end
end
end
Model.append_extensions(ActiveModel::ClassMethods)
Model.append_inclusions(ActiveModel::InstanceMethods)
end
describe 'every active_model compliant object', :shared => true do
it 'a compliant object should respond_to #to_model' do
@object.respond_to?(:to_model).should be_true
end
it 'a compliant object should respond_to #new_record?' do
@object.respond_to?(:new_record?).should be_true
end
it 'a compliant object should respond_to #destroyed?' do
@object.respond_to?(:destroyed?).should be_true
end
it 'a compliant object should respond_to #valid?' do
@object.respond_to?(:valid?).should be_true
end
it 'a compliant object should respond_to #errors' do
@object.respond_to?(:errors).should be_true
end
it 'a compliant object should respond_to #class.model_name' do
@object.class.respond_to?(:model_name).should be_true
end
it '#new_record? should either return true or false' do
(@object.new_record? == true || @object.new_record? == false).should be_true
end
it '#destroyed? should either return true or false' do
(@object.destroyed? == true || @object.destroyed? == false).should be_true
end
it '#valid? should either return true or false' do
(@object.valid? == true || @object.valid? == false).should be_true
end
it '#errors should return an object that responds to #[](field)' do
@object.errors.respond_to?(:[]).should be_true
end
it '#errors should return an object that responds to #full_messages' do
@object.errors.respond_to?(:full_messages).should be_true
end
it '#class.model_name should return an object that responds to #singular' do
@object.class.model_name.respond_to?(:singular).should be_true
end
it '#class.model_name should return an object that responds to #plural' do
@object.class.model_name.respond_to?(:plural).should be_true
end
it '#class.model_name should return an object that responds to #element' do
@object.class.model_name.respond_to?(:element).should be_true
end
it '#class.model_name should return an object that responds to #human' do
@object.class.model_name.respond_to?(:human).should be_true
end
it '#class.model_name should return an object that responds to #collection' do
@object.class.model_name.respond_to?(:collection).should be_true
end
it '#class.model_name should return an object that responds to #partial_path' do
@object.class.model_name.respond_to?(:partial_path).should be_true
end
it '#class.model_name.singular should follow active_model conventions' do
@object.class.model_name.singular.should == @model_name_singular
end
it '#class.model_name.plural should follow active_model conventions' do
@object.class.model_name.plural.should == @model_name_plural
end
it '#class.model_name.element should follow active_model conventions' do
@object.class.model_name.element.should == @model_name_element
end
it '#class.model_name.human should follow active_model conventions' do
@object.class.model_name.human.should == @model_name_human
end
it '#class.model_name.collection should follow active_model conventions' do
@object.class.model_name.collection.should == @model_name_collection
end
it '#class.model_name.partial_path should follow active_model conventions' do
@object.class.model_name.partial_path.should == @model_name_partial_path
end
end
describe 'An active_model compliant DataMapper::Resource' do
before :all do
if ENV['USE_DM_VALIDATIONS']
require 'dm-validations'
end
module ::ComplianceTest
class ProfileInfo
include DataMapper::Resource
property :id, Serial
end
end
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.auto_migrate!
end
before :each do
@object = ComplianceTest::ProfileInfo.create.to_model
@model_name_singular = 'compliance_test_profile_info'
@model_name_plural = 'compliance_test_profile_infos'
@model_name_element = 'profile_info'
@model_name_human = 'profile info'
@model_name_collection = 'compliance_test_profile_infos'
@model_name_partial_path = 'compliance_test_profile_infos/profile_info'
end
it_should_behave_like 'every active_model compliant object'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment