Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using namespaced classed does not work #75

Closed
OneDivZero opened this issue May 8, 2012 · 8 comments
Closed

Using namespaced classed does not work #75

OneDivZero opened this issue May 8, 2012 · 8 comments

Comments

@OneDivZero
Copy link

Hi, I started using rolify but I ran into several problems, if I want to use my User-Class which is namespaced via "Auth::User".

Running: rails generate rolify:role "Auth::Role" "Auth::User"

Causes: a nonworking migrationfile starting with:
class RolifyCreateAuth::Roles < ActiveRecord::Migration # look at the ::Roles

and each table got named with slashed like:
create_table(:auth/roles) do |t|

OK I changed the whole stuff such that is uses underscores instead of slashes and also updates the model-files such that:

class Auth::Role < ActiveRecord::Base
#OLD: has_and_belongs_to_many :auth/users, :join_table => :auth/users_auth/roles
has_and_belongs_to_many :auth_users, :join_table => :auth_users_roles
belongs_to :resource, :polymorphic => true
attr_accessible :name
end

AND

class Auth::User < ActiveRecord::Base

rolify:role_cname => 'Auth::Role'
has_and_belongs_to_many :auth_roles, :join_table => :auth_users_roles
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
end

But when I try to setup a seeds.rb like:

role1 = Auth::Role.create! :name => 'guest'
role2 = Auth::Role.create! :name => 'root'
user1 = Auth::User.create! :email => 'approot@test.de', :password => 'test', :password_confirmation => 'test'
user1.add_role :root

it causes an exception:
SQLite3::SQLException: no such table: auth/users_auth/roles: SELECT 1 FROM "auth_roles" INNER JOIN "auth/users_auth/rol
es" ON "auth_roles"."id" = "auth/users_auth/roles"."role_id" WHERE "auth/users_auth/roles"."user_id" = 1 AND "auth_roles
"."id" = 2 LIMIT 1

It seems that rolify has some hardcoded statements ?! Or what could be the problem ? Have I missed another option ?

Thanks in advance, Micha

@ghost ghost assigned EppO Jul 31, 2012
@julesce
Copy link

julesce commented Sep 27, 2012

I'm also using namespaces, and running into exactly the same problem. If I'm reading the code correctly, the issue lies in lib\rolify.rb, line 21:

rolify_options.merge!({ :join_table => "#{self.to_s.tableize}_#{options[:role_cname].tableize}" }) if Rolify.orm == "active_record"

I reckon I'm going to fork this and then pass the join table through as an option. Will send through pull request if I come right fixing the namespace support.

@EppO
Copy link
Member

EppO commented Oct 22, 2012

Hi,

are you using folder naming convention to split your user classes ? like app/models/auth/user.rb and app/models/auth/role.rb in your case ?
I don't think you should create your table like that: create_table(:auth/roles) but I don't use namespace ActiveRecord models often, I may be wrong.

@OneDivZero
Copy link
Author

Huh long time ago, yes I placed my files according to naming convention. Did you notice the slashes in sql-exception ? I continued using rolify without a namespace, but this is not my prefered 'workaround'. Would be nice, if this get's fixed.

@julesce
Copy link

julesce commented Oct 23, 2012

Hi guys, I've got a fork that is handing namespaces, I just need to get around to submitting a pull request. Not long hopefully :)

@dmonagle
Copy link
Contributor

I have run into this issue myself and have solved it by replacing the use of tabelize on the class name to calling table_name on the class itself. This will always return the correct table name.

I have also updated this in the activerecord connector but have left the MongoDB connector as is as I do not use it and I have no means to test it.

I will attach a pull request for this.

@EppO EppO closed this as completed in f732464 Dec 6, 2012
EppO added a commit that referenced this issue Dec 6, 2012
added specs to test namespace feature
works only with ActiveRecord so far
refs #75
EppO added a commit that referenced this issue Dec 6, 2012
make #112 pass the specs
closes #75
@EppO
Copy link
Member

EppO commented Dec 6, 2012

namespaced models should be supported now. You can look in the specs to see how to use them.
I only tweaked the table_prefix setting in the ActiveRecord model to make it work properly.

@OneDivZero
Copy link
Author

Hey thanks for doing this. But currently I discovered some problems to get this working in the right way:
Im working with this in my Gemfile: gem "rolify", ">= 3.2.0", :git => "https://github.com/EppO/rolify.git"

And first of all, the generators do not work, they still adding slashed instead of underscores to all relevant definitions. When i try to do some seeding, I'm getting this exception:

SQLite3::SQLException: no such column: auth_users_auth_roles.user_id: SELECT 1 FROM "auth_roles" INNER JOIN "auth_users _auth_roles" ON "auth_roles"."id" = "auth_users_auth_roles"."role_id" WHERE "auth_users_auth_roles"."user_id" = 2 AND "auth_roles"."id" = 1 LIMIT 1

So I fixed manually the migration to
t.references :user
t.references :role

and updated all index-defintions. Now I'm able to perform the seeding. But I'm aksing myself, if I could run into more problems, cause of this manually fix ?! Did I miss anything? Thanks in advance

@EppO
Copy link
Member

EppO commented Feb 6, 2013

Right, the generator has not been modified as far as I remember. I will look into it to make them support namespaced models

@EppO EppO reopened this Feb 6, 2013
@EppO EppO closed this as completed in 8002b9c Feb 20, 2013
EppO added a commit that referenced this issue Feb 20, 2013
trying a fixtures reload on each run, seems like travis-ci gets different Mongoid _ids between let! calls and should check
closes #126
fixes #125
fixes #75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants