Skip to content

Commit

Permalink
use an underscored version of the class name for the stub method name…
Browse files Browse the repository at this point in the history
…, not #table_name

git-svn-id: http://ar-code.svn.engineyard.com/plugins/model_stubbing@91 d98f8484-0aa7-43af-80ea-302c351499d6
  • Loading branch information
rick committed Nov 27, 2007
1 parent c0cd42c commit 441253f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
11 changes: 10 additions & 1 deletion lib/model_stubbing/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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|
Expand Down
12 changes: 3 additions & 9 deletions spec/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 441253f

Please sign in to comment.