Permalink
Browse files

use an underscored version of the class name for the stub method name…

…, not #table_name

git-svn-id: http://ar-code.svn.engineyard.com/plugins/model_stubbing@91 d98f8484-0aa7-43af-80ea-302c351499d6
  • Loading branch information...
1 parent c0cd42c commit 441253f84be3764b0974babd84e66f9eaf14b9f6 rick committed Nov 27, 2007
Showing with 14 additions and 10 deletions.
  1. +10 −1 lib/model_stubbing/model.rb
  2. +3 −9 spec/models.rb
  3. +1 −0 spec/spec_helper.rb
@@ -23,7 +23,7 @@ def stub(name = nil, options = {})
def initialize(definition, klass, options = {}, &block)
@definition = definition
@model_class = klass
- @name = options[:name] || klass.table_name.to_sym
+ @name = options[:name] || default_name.to_sym
@plural = options[:plural] || name
@singular = options[:singular] || name.to_s.singularize
@stubs = {}
@@ -38,6 +38,15 @@ class << @model_class
instance_eval &block if block
end
+ def default_name
+ name = @model_class.name
+ if name.respond_to?(:underscore)
+ name.underscore.pluralize.gsub(/\//, '_')
+ else
+ name.downcase.gsub(/::/, '_') << "s"
+ end
+ end
+
def dup(definition = nil)
copy = self.class.new(definition || @definition, @model_class, :name => @name, :plural => @plural, :singular => @singular)
stubs.each do |key, value|
View
@@ -60,16 +60,10 @@ def set_attribute(key, value)
end
end
-User = Class.new BlankModel do
- def self.table_name() @table_name ||= 'users' end
-end
-Post = Class.new BlankModel do
- def self.table_name() @table_name ||= 'posts' end
-end
+User = Class.new BlankModel
+Post = Class.new BlankModel
module Foo
- Bar = Class.new BlankModel do
- def self.table_name() @table_name ||= 'foo_bars' end
- end
+ Bar = Class.new BlankModel
end
ModelStubbing.define_models do
View
@@ -3,6 +3,7 @@
# look for spec as plugin, in lib, or as a gem
$LOAD_PATH << File.join(dir, '..', '..', 'rspec', 'lib')
$LOAD_PATH << File.join(dir, '..', '..', '..', 'rspec', 'lib')
+
begin
require 'spec'
rescue LoadError

0 comments on commit 441253f

Please sign in to comment.