Skip to content

Commit 974041e

Browse files
author
Stefano Verna
committed
First commit
1 parent e4d7b5d commit 974041e

15 files changed

+355
-2
lines changed

Diff for: README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ Or install it yourself as:
1818

1919
## Usage
2020

21-
TODO: Write usage instructions here
21+
```
22+
gem 'chosen-rails'
23+
24+
rake activeadmin_blog:install:migrations
25+
26+
rake db:migrate
27+
```
2228

2329
## Contributing
2430

@@ -27,3 +33,4 @@ TODO: Write usage instructions here
2733
3. Commit your changes (`git commit -am 'Add some feature'`)
2834
4. Push to the branch (`git push origin my-new-feature`)
2935
5. Create new Pull Request
36+

Diff for: activeadmin-blog.gemspec

+7
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ Gem::Specification.new do |spec|
2020

2121
spec.add_development_dependency "bundler", "~> 1.3"
2222
spec.add_development_dependency "rake"
23+
24+
spec.add_dependency "friendly_id"
25+
spec.add_dependency "activeadmin"
26+
spec.add_dependency "activeadmin-gallery"
27+
spec.add_dependency "activeadmin-wysihtml5"
28+
spec.add_dependency "activeadmin-extra"
2329
end
30+

Diff for: app/admin/authors.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ActiveAdmin.register Blog::Author do
2+
menu parent: "Blog", label: "Authors"
3+
4+
index do
5+
column :first_name
6+
column :last_name
7+
default_actions
8+
end
9+
10+
filter :first_name
11+
filter :last_name
12+
13+
form do |f|
14+
f.inputs "Author Details" do
15+
f.input :first_name
16+
f.input :last_name
17+
f.input :bio
18+
end
19+
f.actions
20+
end
21+
end
22+

Diff for: app/admin/categories.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
ActiveAdmin.register Blog::Category do
2+
menu parent: "Blog", label: "Categories"
3+
4+
index do
5+
column :name
6+
default_actions
7+
end
8+
9+
filter :name
10+
filter :description
11+
12+
form do |f|
13+
f.inputs "Category Details" do
14+
f.input :name
15+
f.input :description
16+
end
17+
f.actions
18+
end
19+
end
20+

Diff for: app/admin/posts.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
ActiveAdmin.register Blog::Post do
2+
menu parent: "Blog", label: "Posts"
3+
4+
index do
5+
column :title
6+
column :author_name
7+
column :visible
8+
column :published_at
9+
default_actions
10+
end
11+
12+
filter :title
13+
filter :abstract
14+
filter :body
15+
16+
form do |f|
17+
f.inputs "Post details" do
18+
f.input :category
19+
f.input :author
20+
f.input :title, as: :title
21+
f.input :abstract, as: :wysihtml5, commands: :barebone, height: :small, blocks: :barebone
22+
f.input :body, as: :wysihtml5, height: :huge
23+
f.input :comma_separated_tags, as: :terms, terms: Blog::Tag.all.map(&:name)
24+
f.input :related_posts, as: :chosen, create_option: false
25+
f.input :visible
26+
f.input :published_at, as: :datepicker
27+
end
28+
29+
f.inputs "Featured image" do
30+
f.has_image :featured_image
31+
end
32+
33+
f.inputs "SEO" do
34+
f.input :seo_slug
35+
f.input :seo_title
36+
f.input :seo_description
37+
end
38+
39+
f.actions
40+
end
41+
end
42+

Diff for: app/admin/tags.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ActiveAdmin.register Blog::Tag do
2+
menu parent: "Blog", label: "Tags"
3+
4+
index do
5+
column :name
6+
column :trending
7+
default_actions
8+
end
9+
10+
filter :name
11+
12+
form do |f|
13+
f.inputs "Tag Details" do
14+
f.input :name
15+
f.input :trending
16+
f.input :description
17+
end
18+
f.actions
19+
end
20+
end
21+

Diff for: app/models/blog/author.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Blog
2+
class Author < ActiveRecord::Base
3+
self.table_name = 'blog_authors'
4+
5+
has_many :posts, class_name: "Blog::Post", inverse_of: :author
6+
7+
validates :first_name, :last_name, :bio, presence: true
8+
9+
scope :sorted_alphabetically, -> { order('first_name, last_name') }
10+
11+
attr_accessible :first_name, :last_name, :bio
12+
13+
def full_name
14+
[ first_name, last_name ].join(" ")
15+
end
16+
end
17+
end
18+

Diff for: app/models/blog/category.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Blog
2+
class Category < ActiveRecord::Base
3+
self.table_name = 'blog_categories'
4+
5+
has_many :posts, class_name: "Blog::Post", inverse_of: :category
6+
7+
validates :name, presence: true
8+
validates :name, uniqueness: true
9+
10+
extend FriendlyId
11+
friendly_id :name, use: :slugged
12+
13+
scope :sorted_alphabetically, -> { order('name') }
14+
15+
attr_accessible :name, :description
16+
end
17+
end
18+

Diff for: app/models/blog/post.rb

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module Blog
2+
class Post < ActiveRecord::Base
3+
self.table_name = 'blog_posts'
4+
5+
belongs_to :category, class_name: "Blog::Category", inverse_of: :posts
6+
belongs_to :author, class_name: "Blog::Author", inverse_of: :posts
7+
8+
has_many :taggings, class_name: "Blog::Tagging", inverse_of: :post
9+
has_many :tags, through: :taggings
10+
has_many :post_relations, class_name: "Blog::PostRelation", inverse_of: :post
11+
has_many :related_posts, through: :post_relations, source: :related
12+
13+
validates :title, :body, :category, :author, :published_at, presence: true
14+
validate :check_presence_of_featured_image_if_sticky
15+
16+
extend FriendlyId
17+
friendly_id :seo_slug_or_title, use: :slugged
18+
19+
scope :sorted_by_date, -> { order('published_at DESC') }
20+
scope :sticky, -> { where(sticky: true) }
21+
scope :matching_query, ->(query) { where("title LIKE :query OR body LIKE :query", query: "%#{query}%") }
22+
scope :visible, -> { where(visible: true) }
23+
scope :published, -> { visible.where('published_at < ?', Time.now) }
24+
25+
has_image :featured_image
26+
27+
attr_accessible :category_id, :author_id, :title, :abstract, :body, :sticky,
28+
:visible, :published_at, :seo_slug, :seo_title, :seo_description,
29+
:comma_separated_tags
30+
31+
def seo_slug
32+
slug
33+
end
34+
35+
def seo_slug=(slug)
36+
@seo_slug = slug
37+
end
38+
39+
def seo_slug_or_title
40+
@seo_slug.presence || title
41+
end
42+
43+
def comma_separated_tags
44+
tags.map(&:name).join(',')
45+
end
46+
47+
def comma_separated_tags=(data)
48+
self.tags = []
49+
data.split(/\s*,\s*/).each do |tag_name|
50+
self.tags << Tag.find_or_create!(tag_name)
51+
end
52+
end
53+
54+
def author_name
55+
author.full_name
56+
end
57+
58+
private
59+
60+
def check_presence_of_featured_image_if_sticky
61+
if self.sticky && self.featured_image.nil?
62+
errors.add(:sticky, "richiede la presenza di una featured image")
63+
end
64+
end
65+
end
66+
end
67+

Diff for: app/models/blog/post_relation.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Blog
2+
class PostRelation < ActiveRecord::Base
3+
self.table_name = 'blog_post_relations'
4+
5+
belongs_to :post, class_name: 'Blog::Post'
6+
belongs_to :related, class_name: 'Blog::Post'
7+
8+
validates :post, :related, presence: true
9+
validates :related_id, uniqueness: { scope: :post_id }
10+
end
11+
end
12+

Diff for: app/models/blog/tag.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Blog
2+
class Tag < ActiveRecord::Base
3+
self.table_name = 'blog_tags'
4+
5+
has_many :taggings, class_name: "Blog::Tagging", inverse_of: :tag, dependent: :destroy
6+
has_many :posts, through: :taggings
7+
8+
validates :name, presence: true
9+
validates :name, uniqueness: true
10+
11+
extend FriendlyId
12+
friendly_id :name, use: :slugged
13+
14+
def self.find_or_create!(tag_name)
15+
Tag.where(name: tag_name).first_or_create
16+
end
17+
18+
scope :sorted_alphabetically, -> { order('name') }
19+
scope :trending, -> { where(trending: true) }
20+
scope :with_posts, -> { select('blog_tags.*').joins(:posts).group('blog_tags.id').merge(Post.visible) }
21+
scope :by_usage, -> { with_posts.select('COUNT(blog_tags.id) AS posts_count').order('posts_count DESC') }
22+
scope :non_trending, -> { where(trending: false) }
23+
24+
attr_accessible :name, :trending, :description
25+
end
26+
end
27+

Diff for: app/models/blog/tagging.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Blog
2+
class Tagging < ActiveRecord::Base
3+
self.table_name = 'blog_taggings'
4+
5+
belongs_to :tag, class_name: "Blog::Tag", inverse_of: :taggings
6+
belongs_to :post, class_name: "Blog::Post", inverse_of: :taggings
7+
8+
validates :post, :tag, presence: true
9+
validates :post_id, uniqueness: { scope: :tag_id }
10+
end
11+
end
12+

Diff for: db/migrate/20130201175346_create_activeadmin_blog.rb

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class CreateActiveadminBlog < ActiveRecord::Migration
2+
3+
def change
4+
create_table "blog_authors" do |t|
5+
t.string "first_name"
6+
t.string "last_name"
7+
t.text "bio"
8+
t.datetime "created_at", null: false
9+
t.datetime "updated_at", null: false
10+
end
11+
12+
create_table "blog_categories" do |t|
13+
t.string "name", null: false
14+
t.string "slug", null: false
15+
t.datetime "created_at", null: false
16+
t.datetime "updated_at", null: false
17+
t.text "description"
18+
end
19+
20+
create_table "blog_post_relations" do |t|
21+
t.integer "post_id", null: false
22+
t.integer "related_id", null: false
23+
t.datetime "created_at", null: false
24+
t.datetime "updated_at", null: false
25+
end
26+
27+
add_index "blog_post_relations", ["post_id"], name: "index_post_relations_on_post_id"
28+
add_index "blog_post_relations", ["related_id"], name: "index_post_relations_on_related_id"
29+
30+
create_table "blog_posts" do |t|
31+
t.string "slug", null: false
32+
t.string "title", null: false
33+
t.text "abstract"
34+
t.text "body", null: false
35+
t.integer "category_id", null: false
36+
t.integer "author_id", null: false
37+
t.boolean "sticky", default: false, null: false
38+
t.boolean "visible", default: false, null: false
39+
t.datetime "published_at", null: false
40+
t.datetime "created_at", null: false
41+
t.datetime "updated_at", null: false
42+
t.string "seo_title"
43+
t.text "seo_description"
44+
end
45+
46+
add_index "blog_posts", ["author_id"], name: "index_posts_on_author_id"
47+
add_index "blog_posts", ["category_id"], name: "index_posts_on_category_id"
48+
49+
create_table "blog_taggings" do |t|
50+
t.integer "post_id", null: false
51+
t.integer "tag_id", null: false
52+
end
53+
54+
add_index "blog_taggings", ["tag_id"], name: "index_taggings_on_tag_id"
55+
56+
create_table "blog_tags" do |t|
57+
t.string "name", null: false
58+
t.string "slug", null: false
59+
t.text "description"
60+
t.boolean "trending", default: false, null: false
61+
end
62+
end
63+
64+
end
65+

Diff for: lib/activeadmin/blog.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
require "activeadmin/blog/version"
2+
require "activeadmin/blog/engine"
23

34
module Activeadmin
45
module Blog
5-
# Your code goes here...
66
end
77
end
8+

Diff for: lib/activeadmin/blog/engine.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Activeadmin
2+
module Blog
3+
class Engine < ::Rails::Engine
4+
isolate_namespace Activeadmin::Blog
5+
engine_name 'activeadmin_blog'
6+
7+
initializer "load admin" do
8+
puts File.expand_path('../../../app/admin', File.dirname(__FILE__))
9+
ActiveAdmin.application.load_paths += [ File.expand_path('../../../app/admin', File.dirname(__FILE__)) ]
10+
end
11+
end
12+
end
13+
end
14+

0 commit comments

Comments
 (0)