diff options
-rw-r--r-- | app/models/blog_category.rb | 2 | ||||
-rw-r--r-- | app/models/blog_comment.rb | 4 | ||||
-rw-r--r-- | app/models/blog_post.rb | 3 | ||||
-rw-r--r-- | generators/refinery_blog/refinery_blog_generator.rb | 41 | ||||
-rw-r--r-- | generators/refinery_blog/templates/migration.rb | 7 |
5 files changed, 36 insertions, 21 deletions
diff --git a/app/models/blog_category.rb b/app/models/blog_category.rb index 9060d11..09d32de 100644 --- a/app/models/blog_category.rb +++ b/app/models/blog_category.rb @@ -1,5 +1,7 @@ class BlogCategory < ActiveRecord::Base + has_and_belongs_to_many :posts, :class_name => 'BlogPost' + acts_as_indexed :fields => [:title] validates_presence_of :title diff --git a/app/models/blog_comment.rb b/app/models/blog_comment.rb index f943ce1..7c6b51f 100644 --- a/app/models/blog_comment.rb +++ b/app/models/blog_comment.rb @@ -1,8 +1,12 @@ class BlogComment < ActiveRecord::Base + belongs_to :post, :class_name => 'BlogPost' + acts_as_indexed :fields => [:name, :email, :body] validates_presence_of :title validates_uniqueness_of :title + named_scope :approved, :conditions => {:approved => true} + named_scope :rejected, :conditions => {:approved => false} end diff --git a/app/models/blog_post.rb b/app/models/blog_post.rb index 481b8cb..bd12f22 100644 --- a/app/models/blog_post.rb +++ b/app/models/blog_post.rb @@ -1,5 +1,8 @@ class BlogPost < ActiveRecord::Base + has_many :comments, :class_name => 'BlogComment' + has_and_belongs_to_many :categories, :class_name => 'BlogCategory' + acts_as_indexed :fields => [:title, :body] validates_presence_of :title diff --git a/generators/refinery_blog/refinery_blog_generator.rb b/generators/refinery_blog/refinery_blog_generator.rb index 0739498..d18268b 100644 --- a/generators/refinery_blog/refinery_blog_generator.rb +++ b/generators/refinery_blog/refinery_blog_generator.rb @@ -7,37 +7,44 @@ class RefineryBlogGenerator < Rails::Generator::NamedBase end def banner - "Usage: script/generate refinery_blog" + 'Usage: script/generate refinery_blog' end def manifest - tables = %w(posts comments categories).map{|t| "blog_#{t}"} record do |m| m.template('seed.rb', 'db/seeds/refinerycms_blog.rb') m.migration_template('migration.rb', 'db/migrate', - :migration_file_name => "create_blog_structure", + :migration_file_name => 'create_blog_structure', :assigns => { - :migration_name => "CreateBlogStructure", + :migration_name => 'CreateBlogStructure', :tables => [{ - :table_name => tables.first, + :table_name => 'blog_posts', :attributes => [ - Rails::Generator::GeneratedAttribute.new("title", "string"), - Rails::Generator::GeneratedAttribute.new("body", "text"), - Rails::Generator::GeneratedAttribute.new("draft", "boolean") - ] + Rails::Generator::GeneratedAttribute.new('title', 'string'), + Rails::Generator::GeneratedAttribute.new('body', 'text'), + Rails::Generator::GeneratedAttribute.new('draft', 'boolean') + ], :id => true },{ - :table_name => tables.second, + :table_name => 'blog_comments', :attributes => [ - Rails::Generator::GeneratedAttribute.new("name", "string"), - Rails::Generator::GeneratedAttribute.new("email", "string"), - Rails::Generator::GeneratedAttribute.new("body", "text") - ] + Rails::Generator::GeneratedAttribute.new('name', 'string'), + Rails::Generator::GeneratedAttribute.new('email', 'string'), + Rails::Generator::GeneratedAttribute.new('body', 'text'), + Rails::Generator::GeneratedAttribute.new('approved', 'boolean'), + Rails::Generator::GeneratedAttribute.new('blog_post_id', 'integer') + ], :id => true },{ - :table_name => tables.third, + :table_name => 'blog_categories', :attributes => [ - Rails::Generator::GeneratedAttribute.new("title", "string") - ] + Rails::Generator::GeneratedAttribute.new('title', 'string') + ], :id => true + },{ + :table_name => 'blog_categories_blog_posts', + :attributes => [ + Rails::Generator::GeneratedAttribute.new('blog_category_id', 'integer'), + Rails::Generator::GeneratedAttribute.new('blog_post_id', 'integer') + ], :id => false }] }) end diff --git a/generators/refinery_blog/templates/migration.rb b/generators/refinery_blog/templates/migration.rb index 2ebd06e..5ba29c6 100644 --- a/generators/refinery_blog/templates/migration.rb +++ b/generators/refinery_blog/templates/migration.rb @@ -1,16 +1,15 @@ class <%= migration_name %> < ActiveRecord::Migration def self.up<% tables.each do |table| %> - create_table :<%= table[:table_name] %> do |t| + create_table :<%= table[:table_name] %>, :id => <%= table[:id].to_s %> do |t| <% table[:attributes].each do |attribute| -%> t.<%= attribute.type %> :<%= attribute.name %> <% end -%> - t.timestamps + <%= 't.timestamps' if table[:id] %> end - add_index :<%= table[:table_name] %>, :id + <%= "add_index :#{table[:table_name]}, :id" if table[:id] %> <% end -%> - load(Rails.root.join('db', 'seeds', 'refinerycms_blog.rb').to_s) end |