Skip to content

Commit

Permalink
Don't overwrite signatures of existing methods in AR classes
Browse files Browse the repository at this point in the history
  • Loading branch information
wrozka committed Jun 11, 2013
1 parent 253d3a2 commit f0838e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
10 changes: 9 additions & 1 deletion lib/bogus/active_record_accessors.rb
Expand Up @@ -11,7 +11,7 @@ class ActiveRecordAccessors

def all
return [] unless klass < ActiveRecord::Base
klass.columns.map(&:name)
return missing_attributes
end

def get(name)
Expand All @@ -24,6 +24,14 @@ def instance_methods
@instance_methods.call(klass)
end

def all_attributes
klass.columns.map(&:name).map(&:to_sym)
end

def missing_attributes
all_attributes - instance_methods.all
end

class Attribute < Struct.new(:name)
def parameters
[]
Expand Down
20 changes: 6 additions & 14 deletions spec/bogus/fake_ar_attributes_spec.rb
Expand Up @@ -14,8 +14,8 @@
end

class BlogPost < ActiveRecord::Base
def author
"#{self[:author]}!"
def author(x)
"#{x} #{self[:author]}!"
end
end

Expand Down Expand Up @@ -43,20 +43,12 @@ def author
}.to raise_error(NameError)
end

it "does not break the regular behavior of overwritten methods" do
post = BlogPost.new
it "does not overwrite existing method signatures" do
post = fake(:blog_post)

post.name = "hello world"
post.author("hello")

post.name.should == "hello world"
end

it "does not overwrite existing methods" do
post = BlogPost.new

post.author = "Batman"

post.author.should == "Batman!"
post.should have_received.author("hello")
end

class ExampleForActiveRecordAttributes
Expand Down

0 comments on commit f0838e9

Please sign in to comment.