Skip to content

Commit

Permalink
! Adding column status to posts table.Adding scope to Post index page…
Browse files Browse the repository at this point in the history
…. Customze the Show page.Adding Dashboard sections.
  • Loading branch information
williamn committed Jul 19, 2012
1 parent f34f437 commit 5a6d618
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
13 changes: 13 additions & 0 deletions aa-blog/app/admin/dashboards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@
# section "Membership Summary", :if => :memberships_enabled?
# section "Membership Summary", :if => Proc.new { current_admin_user.account.memberships.any? }

section "Draft posts" do
table_for Post.where("status = ?", "Draft") do
column(:title) { |post| link_to post.title, admin_post_path(post) }
column(:category)
end
end

section "Recent published posts" do
table_for Post.where("status = ?", "Published") do
column(:title) { |post| link_to post.title, admin_post_path(post) }
column(:category)
end
end
end
40 changes: 40 additions & 0 deletions aa-blog/app/admin/posts.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
ActiveAdmin.register Post do
scope :all, :default => true
Post::STATUS.each do |status|
scope status.to_sym do |posts|
posts.where("status = ?", status)
end
end

form do |f|
f.inputs "New Post" do
f.input :title
f.input :status, :as => :select, :collection => Post::STATUS
f.input :category
f.input :body
f.buttons
end
end

index do
column :title
default_actions
end

show do |post|
attributes_table do
row :title
row :category
row :status do |post|
status_tag post.status, (post.status == "Published" ? :ok : :warning)
end
row :body
end
active_admin_comments
end

sidebar "Other Post for this Category", :only => :show do
ul do
Post.where("id IS NOT ? AND category_id = ?", resource.id, resource.category.id).each do |post|
li link_to post.title, admin_post_path(post)
end
end
end
end
1 change: 1 addition & 0 deletions aa-blog/app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Category < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true
has_many :posts
end
4 changes: 3 additions & 1 deletion aa-blog/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Post < ActiveRecord::Base
attr_accessible :body, :title, :category_id
STATUS = %w(Draft Published)
attr_accessible :body, :title, :category_id, :status
validates :title, :body, :presence => true
validates :status, :inclusion => { :in => self::STATUS }
belongs_to :category
end
5 changes: 5 additions & 0 deletions aa-blog/db/migrate/20120719061158_add_status_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToPosts < ActiveRecord::Migration
def change
add_column :posts, :status, :string
end
end
3 changes: 2 additions & 1 deletion aa-blog/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120710134534) do
ActiveRecord::Schema.define(:version => 20120719061158) do

create_table "active_admin_comments", :force => true do |t|
t.string "resource_id", :null => false
Expand Down Expand Up @@ -58,6 +58,7 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "category_id"
t.string "status"
end

end

0 comments on commit 5a6d618

Please sign in to comment.