Additional checks #82

Closed
wants to merge 1 commit into
from

4 participants

@adie

First check is to support models with no primary key


Also I changed c.is_a?(Class) to Class === c because of the following error:

Unable to annotate my_model.rb: wrong number of arguments(2 for 1)
(.../activesupport-3.2.5/lib/active_support/option_merger.rb:22:in `is_a?')

I didn't find the reason of that error, just tried Class === c and it worked.


And I had to add the last check because of some strange errors from factory_girl:

Unable to annotate my_model.rb: undefined method `include?' for
#<FactoryGirl::Declaration::Implicit:0x9615d74> 
(.../lib/annotate/annotate_models.rb:322:in `block in get_loaded_model')

I've found that it was trying to get ancestors for instance of FactoryGirl::DefinitionProxy, which seems to be making a lot of magic.


@alexch
Collaborator
@alexch

How does one create an ActiveRecord object with no primary key anyway? Can you write (or at least describe) a unit test for this?

@adie

In the migration you can write

create_table :items_users, :id => false do |t|
  t.references :item
  t.references :user
end

When you create habtm associations, you need such a migration, and in some odd cases you may need the model itself for this association.

@arturtr arturtr commented on the diff Jun 18, 2012
lib/annotate/annotate_models.rb
@@ -82,7 +82,7 @@ def get_schema_info(klass, header, options = {})
attrs = []
attrs << "default(#{quote(col.default)})" unless col.default.nil?
attrs << "not null" unless col.null
- attrs << "primary key" if col.name.to_sym == klass.primary_key.to_sym
+ attrs << "primary key" if klass.primary_key && col.name.to_sym == klass.primary_key.to_sym
@arturtr
arturtr added a line comment Jun 18, 2012

I solve this problem this way:

attrs << "primary key" if col.name.to_sym == klass.primary_key.try(:to_sym)
@alexch
Collaborator
alexch added a line comment Jul 13, 2012

"try" seems like a fun method. Where is it defined?

@alexch
Collaborator
alexch added a line comment Jul 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jmcnevin

Would really like to see this merged. I can't annotate any Factory Girl factories without it.

@nilbus

@adie Thanks for fixing the is_a? bug - I was running into that too.

@alexch
Collaborator

Done. Sadly my git fu was not strong enough to properly credit adie so her fork will appear to be dangling, but the fix is in 076e778

@alexch alexch closed this Jul 27, 2012
@lime lime pushed a commit to lime/annotate_models that referenced this pull request Nov 5, 2013
@alexch alexch Integrate various fixes around finding model classes, including ctran#82 076e778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment