Skip to content

Commit 974041e

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

File tree

15 files changed

+355
-2
lines changed

15 files changed

+355
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
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+

activeadmin-blog.gemspec

Lines changed: 7 additions & 0 deletions
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+

app/admin/authors.rb

Lines changed: 22 additions & 0 deletions
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+

app/admin/categories.rb

Lines changed: 20 additions & 0 deletions
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+

app/admin/posts.rb

Lines changed: 42 additions & 0 deletions
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+

app/admin/tags.rb

Lines changed: 21 additions & 0 deletions
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+

app/models/blog/author.rb

Lines changed: 18 additions & 0 deletions
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+

app/models/blog/category.rb

Lines changed: 18 additions & 0 deletions
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+

app/models/blog/post.rb

Lines changed: 67 additions & 0 deletions
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+

app/models/blog/post_relation.rb

Lines changed: 12 additions & 0 deletions
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+

0 commit comments

Comments
 (0)