Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding support for Refinery::Blog.user_class. This will enable the Re…

…finery::Blog engine to work out-of-box with existing Rails applications.

This issue was tracked in #286.
  • Loading branch information...
commit caa195740aeb2de006391fb4475ab7965bf695e9 1 parent cab146a
jseitel authored
2  app/models/refinery/blog/post.rb
View
@@ -11,7 +11,7 @@ class Post < ActiveRecord::Base
default_scope :order => 'published_at DESC'
- belongs_to :author, :class_name => 'Refinery::User', :foreign_key => :user_id, :readonly => true
+ belongs_to :author, :class_name => Refinery::Blog.user_class.to_s, :foreign_key => :user_id, :readonly => true
has_many :comments, :dependent => :destroy, :foreign_key => :blog_post_id
acts_as_taggable
2  app/views/refinery/blog/admin/posts/_form.html.erb
View
@@ -102,7 +102,7 @@
<%= f.label :user_id, t('.author') %>
<%= refinery_help_tag t('.author_help') %>
<br/>
- <%= f.collection_select :user_id, ::Refinery::User.all, :id, :username %>
+ <%= f.collection_select :user_id, Refinery::Blog.user_class.all, :id, :username %>
</span>
</div>
3  lib/generators/refinery/blog/templates/config/initializers/refinery/blog.rb.erb
View
@@ -10,4 +10,7 @@ Refinery::Blog.configure do |config|
# config.share_this_key = <%= Refinery::Blog.share_this_key.inspect %>
# config.page_url = <%= Refinery::Blog.page_url.inspect %>
+
+ # If you're grafting onto an existing app, change this to your User class
+ # Refinery::Blog.user_class = User
end
15 lib/refinery/blog/configuration.rb
View
@@ -5,11 +5,26 @@ module Blog
config_accessor :validate_source_url, :comments_per_page, :posts_per_page,
:post_teaser_length, :share_this_key, :page_url
+
self.validate_source_url = false
self.comments_per_page = 10
self.posts_per_page = 10
self.post_teaser_length = 250
self.share_this_key = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
self.page_url = "/blog"
+
+ # Refinery::User isn't available when this line gets hit, so we use static methods instead
+ @@user_class_name = nil
+ def self.user_class= (class_name)
+ if class_name.class == Class
+ @@user_class_name = class_name
+ else
+ raise TypeError, "Expecting configuration input of type Class."
+ end
+ end
+
+ def self.user_class
+ @@user_class_name || Refinery::User
+ end
end
end

2 comments on commit caa1957

wemerson

Thanks for this. It solved my problem using the blog engine with an existing app with its own User class. The one thing I had to change is lib/refinery/blog/configuration.rb line 27. I needed User instead of Refinery::User. Also my User table did not have a username field so I had to create a method for it concatenating first and last names.

Thanks! this was a big help. Hope it gets merged.

jseitel
Owner

The one thing I had to change is lib/refinery/blog/configuration.rb line 27

You shouldn't have to change configuration.rb. Instead, go to config/initializers/refinery.rb and uncomment line #15. That config setting is the whole point of the change. :)

Also my User table did not have a username field so I had to create a method for it concatenating first and last names.

Yep, I had to do the same thing. The framework seems to assume the existence of the property.

Thanks! this was a big help. Hope it gets merged.

Thank you! Glad it was helpful. My first contribution on Github. :)

Please sign in to comment.
Something went wrong with that request. Please try again.