effective_posts 1.1.9 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +4 -9
- data/app/controllers/admin/posts_controller.rb +10 -125
- data/app/controllers/effective/posts_controller.rb +20 -15
- data/app/datatables/effective_posts_datatable.rb +3 -1
- data/app/helpers/effective_posts_helper.rb +8 -27
- data/app/mailers/effective/posts_mailer.rb +2 -1
- data/app/models/effective/post.rb +45 -60
- data/app/views/admin/posts/_form.html.haml +8 -11
- data/app/views/admin/posts/excerpts.html.haml +2 -2
- data/app/views/effective/posts/_form.html.haml +2 -3
- data/app/views/effective/posts/_post.html.haml +1 -1
- data/app/views/effective/posts/_recent_posts.html.haml +1 -4
- data/app/views/effective/posts/_sidebar.html.haml +1 -2
- data/app/views/effective/posts/show.html.haml +1 -2
- data/app/views/effective/posts/submitted.html.haml +1 -1
- data/config/effective_posts.rb +2 -37
- data/config/routes.rb +7 -10
- data/db/migrate/01_create_effective_posts.rb.erb +1 -2
- data/lib/effective_posts.rb +14 -49
- data/lib/effective_posts/engine.rb +1 -1
- data/lib/effective_posts/version.rb +1 -1
- metadata +9 -60
- data/app/assets/javascripts/effective/snippets/read_more_divider.js.coffee +0 -22
- data/app/helpers/effective_truncate_html_helper.rb +0 -73
- data/app/models/effective/access_denied.rb +0 -17
- data/app/models/effective/datatables/posts.rb +0 -40
- data/app/models/effective/snippets/read_more_divider.rb +0 -11
- data/app/views/admin/posts/_actions.html.haml +0 -13
- data/app/views/admin/posts/edit.html.haml +0 -3
- data/app/views/admin/posts/new.html.haml +0 -3
- data/app/views/effective/snippets/_read_more_divider.html.haml +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7fa99747e0baf6c175224b5d3ce645964ac5592137857a6cb28ae6d17687146
|
4
|
+
data.tar.gz: 277fa3f03c1411d810f0dc09663f4deb6bb2be2c1a890cbf7cc88bc9d6b0bd9e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efce23b1c3b45e66acbe5d5b055e114a7baa09add399a29a4a76074c2be854b46565deb2dfc3e9bd3f9559b6e6ec8c51a0fcb66f184c278f8edd8fed10578d18
|
7
|
+
data.tar.gz: 2422db33dc37d6d3f39b3b206c7ea77cf0238a3c9194b44c7b4335058e870e1082449027bab23b71e46177c36e62d697a704359e3fecf7891c7921eef2a000fa
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -2,17 +2,17 @@
|
|
2
2
|
|
3
3
|
A blog implementation with WYSIWYG content editing, post scheduling, pagination and optional top level routes for each post category.
|
4
4
|
|
5
|
-
## effective_posts
|
5
|
+
## effective_posts 2.0
|
6
6
|
|
7
|
-
This is the
|
7
|
+
This is the 2.0 series of effective_posts.
|
8
8
|
|
9
|
-
This requires Twitter Bootstrap 4 and Rails
|
9
|
+
This requires Twitter Bootstrap 4 and Rails 6+
|
10
10
|
|
11
11
|
Please check out [Effective Posts 0.x](https://github.com/code-and-effect/effective_posts/tree/bootstrap3) for more information using this gem with Bootstrap 3.
|
12
12
|
|
13
13
|
## Getting Started
|
14
14
|
|
15
|
-
Please first install the [
|
15
|
+
Please first install the [effective_datatables](https://github.com/code-and-effect/effective_datatables) gem.
|
16
16
|
|
17
17
|
Please download and install [Twitter Bootstrap4](http://getbootstrap.com)
|
18
18
|
|
@@ -64,8 +64,6 @@ You can schedule a post to appear at a later date by setting the published_at va
|
|
64
64
|
|
65
65
|
As well, if you're using the [effective_roles](https://github.com/code-and-effect/effective_roles) gem, you will be able to configure permissions so that only permitted users may view this post.
|
66
66
|
|
67
|
-
Once you click `Save and Edit Content` you will be brought into the effective_regions editor where you may enter the content for your post. Click `Insert Snippet` -> `Read more divider` from the toolbar to place a divider into your post. Only the content above the Read more divider, the excerpt content, will be displayed on any posts#index screens. The full content will be displayed on the posts#show screen.
|
68
|
-
|
69
67
|
|
70
68
|
## Category Routes
|
71
69
|
|
@@ -78,9 +76,6 @@ If disabled, all posts will be available at `/posts`, with posts for a specific
|
|
78
76
|
|
79
77
|
Use `link_to_post_category(:blog)` to display a link to the Blog page. The helper considers `config.use_category_routes` and puts in the correct url.
|
80
78
|
|
81
|
-
Use `post_excerpt(post)` to display the excerpt for a post. Or `post_excerpt(post, :length => 200)` to truncate it and add a Read more link where appropriate.
|
82
|
-
|
83
|
-
|
84
79
|
## Pagination
|
85
80
|
|
86
81
|
The [effective_bootstrap](https://github.com/code-and-effect/effective_bootstrap) gem is used for pagination on all posts#index type screens.
|
@@ -1,136 +1,21 @@
|
|
1
1
|
module Admin
|
2
2
|
class PostsController < ApplicationController
|
3
|
-
before_action(:authenticate_user!)
|
3
|
+
before_action(:authenticate_user!) if defined?(Devise)
|
4
|
+
before_action { EffectiveResources.authorize!(self, :admin, :effective_posts) }
|
4
5
|
|
5
|
-
|
6
|
+
include Effective::CrudController
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
@datatable = EffectivePostsDatatable.new(self)
|
10
|
-
|
11
|
-
authorize_effective_posts!
|
12
|
-
end
|
13
|
-
|
14
|
-
def new
|
15
|
-
@post = Effective::Post.new(published_at: Time.zone.now)
|
16
|
-
@page_title = 'New Post'
|
17
|
-
|
18
|
-
authorize_effective_posts!
|
19
|
-
end
|
20
|
-
|
21
|
-
def create
|
22
|
-
@post = Effective::Post.new(post_params)
|
23
|
-
@post.user = current_user if defined?(current_user)
|
24
|
-
|
25
|
-
@page_title = 'New Post'
|
26
|
-
|
27
|
-
authorize_effective_posts!
|
28
|
-
|
29
|
-
if @post.save
|
30
|
-
if params[:commit] == 'Save and Edit Content'
|
31
|
-
redirect_to effective_regions.edit_path(effective_posts.post_path(@post), :exit => effective_posts.edit_admin_post_path(@post))
|
32
|
-
elsif params[:commit] == 'Save and Add New'
|
33
|
-
flash[:success] = 'Successfully created post'
|
34
|
-
redirect_to effective_posts.new_admin_post_path
|
35
|
-
elsif params[:commit] == 'Save and View'
|
36
|
-
redirect_to effective_posts.post_path(@post)
|
37
|
-
else
|
38
|
-
flash[:success] = 'Successfully created post'
|
39
|
-
redirect_to effective_posts.edit_admin_post_path(@post)
|
40
|
-
end
|
41
|
-
else
|
42
|
-
flash.now[:danger] = 'Unable to create post'
|
43
|
-
render :action => :new
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
def edit
|
48
|
-
@post = Effective::Post.find(params[:id])
|
49
|
-
@page_title = 'Edit Post'
|
50
|
-
|
51
|
-
authorize_effective_posts!
|
52
|
-
end
|
53
|
-
|
54
|
-
def update
|
55
|
-
@post = Effective::Post.find(params[:id])
|
56
|
-
@page_title = 'Edit Post'
|
57
|
-
|
58
|
-
authorize_effective_posts!
|
59
|
-
|
60
|
-
if @post.update_attributes(post_params)
|
61
|
-
if params[:commit] == 'Save and Edit Content'
|
62
|
-
redirect_to effective_regions.edit_path(effective_posts.post_path(@post), :exit => effective_posts.edit_admin_post_path(@post))
|
63
|
-
elsif params[:commit] == 'Save and Add New'
|
64
|
-
flash[:success] = 'Successfully updated post'
|
65
|
-
redirect_to effective_posts.new_admin_post_path
|
66
|
-
elsif params[:commit] == 'Save and View'
|
67
|
-
redirect_to effective_posts.post_path(@post)
|
68
|
-
elsif params[:commit] == 'Duplicate'
|
69
|
-
begin
|
70
|
-
post = @post.duplicate!
|
71
|
-
flash[:success] = 'Successfully saved and duplicated post.'
|
72
|
-
flash[:info] = "You are now editing the duplicated post. This new post has been created as a Draft."
|
73
|
-
rescue => e
|
74
|
-
flash.delete(:success)
|
75
|
-
flash[:danger] = "Unable to duplicate post: #{e.message}"
|
76
|
-
end
|
77
|
-
|
78
|
-
redirect_to effective_posts.edit_admin_post_path(post || @post)
|
79
|
-
else
|
80
|
-
flash[:success] = 'Successfully updated post'
|
81
|
-
redirect_to effective_posts.edit_admin_post_path(@post)
|
82
|
-
end
|
83
|
-
else
|
84
|
-
flash.now[:danger] = 'Unable to update post'
|
85
|
-
render :action => :edit
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def destroy
|
90
|
-
@post = Effective::Post.find(params[:id])
|
91
|
-
|
92
|
-
authorize_effective_posts!
|
93
|
-
|
94
|
-
if @post.destroy
|
95
|
-
flash[:success] = 'Successfully deleted post'
|
96
|
-
else
|
97
|
-
flash[:danger] = 'Unable to delete post'
|
98
|
-
end
|
99
|
-
|
100
|
-
redirect_to effective_posts.admin_posts_path
|
101
|
-
end
|
102
|
-
|
103
|
-
def approve
|
104
|
-
@post = Effective::Post.find(params[:id])
|
105
|
-
@page_title = 'Approve Post'
|
106
|
-
|
107
|
-
authorize_effective_posts!
|
108
|
-
|
109
|
-
if @post.update_attributes(draft: false)
|
110
|
-
flash[:success] = 'Successfully approved post. It is now displayed on the website.'
|
111
|
-
else
|
112
|
-
flash[:danger] = "Unable to approve post: #{@post.errors.full_messages.join(', ')}"
|
113
|
-
end
|
114
|
-
|
115
|
-
redirect_to(:back) rescue redirect_to(effective_posts.admin_posts_path)
|
116
|
-
end
|
117
|
-
|
118
|
-
def excerpts
|
119
|
-
@posts = Effective::Post.includes(:regions)
|
120
|
-
@page_title = 'Post Excerpts'
|
121
|
-
|
122
|
-
authorize_effective_posts!
|
8
|
+
if (config = EffectivePosts.layout)
|
9
|
+
layout(config.kind_of?(Hash) ? config[:admin] : config)
|
123
10
|
end
|
124
11
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
EffectivePosts.authorize!(self, action_name.to_sym, @post || Effective::Post)
|
130
|
-
end
|
12
|
+
submit :save, 'Save'
|
13
|
+
submit :save, 'Save and Add New', redirect: :new
|
14
|
+
submit :save, 'Save and View', redirect: -> { effective_posts.post_path(resource) }
|
15
|
+
submit :save, 'Duplicate', redirect: -> { effective_posts.new_admin_post_path(duplicate_id: resource.id) }
|
131
16
|
|
132
17
|
def post_params
|
133
|
-
params.require(:effective_post).permit
|
18
|
+
params.require(:effective_post).permit!
|
134
19
|
end
|
135
20
|
|
136
21
|
end
|
@@ -1,15 +1,21 @@
|
|
1
1
|
module Effective
|
2
2
|
class PostsController < ApplicationController
|
3
|
-
|
3
|
+
if defined?(Devise)
|
4
|
+
before_action :authenticate_user!, only: [:new, :create, :edit, :update],
|
5
|
+
if: -> { EffectivePosts.submissions_require_current_user }
|
6
|
+
end
|
7
|
+
|
8
|
+
include Effective::CrudController
|
4
9
|
|
5
|
-
|
6
|
-
|
10
|
+
if (config = EffectivePosts.layout)
|
11
|
+
layout(config.kind_of?(Hash) ? (config[:application] || config[:posts]) : config)
|
12
|
+
end
|
7
13
|
|
8
14
|
def index
|
9
15
|
@posts ||= Effective::Post.posts(
|
10
16
|
user: current_user,
|
11
17
|
category: params[:category],
|
12
|
-
unpublished:
|
18
|
+
unpublished: EffectiveResources.authorized?(self, :admin, :effective_posts)
|
13
19
|
)
|
14
20
|
|
15
21
|
@posts = @posts.paginate(page: params[:page])
|
@@ -23,28 +29,27 @@ module Effective
|
|
23
29
|
@posts = @posts.where(search) if search.present?
|
24
30
|
end
|
25
31
|
|
26
|
-
|
32
|
+
EffectiveResources.authorize!(self, :index, Effective::Post)
|
27
33
|
|
28
34
|
@page_title ||= [(params[:category] || 'Blog').to_s.titleize, (" - Page #{params[:page]}" if params[:page])].compact.join
|
29
35
|
@canonical_url ||= helpers.effective_post_category_url(params[:category], page: params[:page])
|
30
36
|
end
|
31
37
|
|
32
38
|
def show
|
33
|
-
@posts ||= Effective::Post.posts(user: current_user, category: params[:category], unpublished:
|
39
|
+
@posts ||= Effective::Post.posts(user: current_user, category: params[:category], unpublished: EffectiveResources.authorized?(self, :admin, :effective_posts))
|
34
40
|
@post = @posts.find(params[:id])
|
35
41
|
|
36
42
|
if @post.respond_to?(:roles_permit?)
|
37
43
|
raise Effective::AccessDenied.new('Access Denied', :show, @post) unless @post.roles_permit?(current_user)
|
38
44
|
end
|
39
45
|
|
40
|
-
|
46
|
+
EffectiveResources.authorize!(self, :show, @post)
|
41
47
|
|
42
|
-
if
|
48
|
+
if EffectiveResources.authorized?(self, :admin, :effective_posts)
|
43
49
|
flash.now[:warning] = [
|
44
50
|
'Hi Admin!',
|
45
51
|
('You are viewing a hidden post.' unless @post.published?),
|
46
52
|
'Click here to',
|
47
|
-
("<a href='#{effective_regions.edit_path(effective_posts.post_path(@post, exit: effective_posts.post_path(@post)))}' class='alert-link' data-no-turbolink='true' data-turbolinks='false'>edit post content</a> or" unless admin_edit?),
|
48
53
|
("<a href='#{effective_posts.edit_admin_post_path(@post)}' class='alert-link'>edit post settings</a>.")
|
49
54
|
].compact.join(' ')
|
50
55
|
end
|
@@ -59,7 +64,7 @@ module Effective
|
|
59
64
|
@post ||= Effective::Post.new(published_at: Time.zone.now)
|
60
65
|
@page_title = 'New Post'
|
61
66
|
|
62
|
-
|
67
|
+
EffectiveResources.authorize!(self, :new, @post)
|
63
68
|
end
|
64
69
|
|
65
70
|
def create
|
@@ -67,7 +72,7 @@ module Effective
|
|
67
72
|
@post.user = current_user if defined?(current_user)
|
68
73
|
@post.draft = (EffectivePosts.submissions_require_approval == true)
|
69
74
|
|
70
|
-
|
75
|
+
EffectiveResources.authorize!(self, :create, @post)
|
71
76
|
|
72
77
|
if @post.save
|
73
78
|
@page_title ||= 'Post Submitted'
|
@@ -89,7 +94,7 @@ module Effective
|
|
89
94
|
@post ||= Effective::Post.find(params[:id])
|
90
95
|
@page_title ||= 'Edit Post'
|
91
96
|
|
92
|
-
|
97
|
+
EffectiveResources.authorize!(self, :edit, @post)
|
93
98
|
end
|
94
99
|
|
95
100
|
def update
|
@@ -97,7 +102,7 @@ module Effective
|
|
97
102
|
draft_was = @post.draft
|
98
103
|
@post.draft = (EffectivePosts.submissions_require_approval == true)
|
99
104
|
|
100
|
-
|
105
|
+
EffectiveResources.authorize!(self, :update, @post)
|
101
106
|
|
102
107
|
if @post.update_attributes(post_params)
|
103
108
|
@page_title ||= 'Post Submitted'
|
@@ -118,7 +123,7 @@ module Effective
|
|
118
123
|
def destroy
|
119
124
|
@post ||= Effective::Post.find(params[:id])
|
120
125
|
|
121
|
-
|
126
|
+
EffectiveResources.authorize!(self, :destroy, @post)
|
122
127
|
|
123
128
|
if @post.destroy
|
124
129
|
flash[:success] = 'Successfully deleted post'
|
@@ -136,7 +141,7 @@ module Effective
|
|
136
141
|
end
|
137
142
|
|
138
143
|
def admin_edit?
|
139
|
-
|
144
|
+
EffectiveResources.authorized?(self, :admin, :effective_posts) && (params[:edit].to_s == 'true')
|
140
145
|
end
|
141
146
|
|
142
147
|
end
|
@@ -25,7 +25,9 @@ class EffectivePostsDatatable < Effective::Datatable
|
|
25
25
|
|
26
26
|
col :created_at, label: 'Submitted at', visible: false
|
27
27
|
|
28
|
-
actions_col
|
28
|
+
actions_col do |post|
|
29
|
+
dropdown_link_to('View', effective_post_path(post), target: '_blank')
|
30
|
+
end
|
29
31
|
end
|
30
32
|
|
31
33
|
collection do
|
@@ -3,7 +3,7 @@ require 'cgi'
|
|
3
3
|
module EffectivePostsHelper
|
4
4
|
|
5
5
|
def effective_posts_header_tags
|
6
|
-
return unless @post
|
6
|
+
return unless @post && @post.kind_of?(Effective::Post) && @post.persisted?
|
7
7
|
|
8
8
|
@effective_pages_og_type = 'article'
|
9
9
|
|
@@ -31,14 +31,15 @@ module EffectivePostsHelper
|
|
31
31
|
def effective_post_category_path(category, opts = nil)
|
32
32
|
return effective_posts.posts_path(opts || {}) unless category.present?
|
33
33
|
|
34
|
-
category = category.to_s
|
34
|
+
category = category.to_s
|
35
|
+
category_path = category.to_s.downcase.parameterize
|
35
36
|
opts = (opts || {}).compact
|
36
37
|
query = ('?' + opts.to_query) if opts.present?
|
37
38
|
|
38
39
|
if EffectivePosts.use_blog_routes
|
39
|
-
"/blog/category/#{
|
40
|
+
"/blog/category/#{category_path}#{query}"
|
40
41
|
elsif EffectivePosts.use_category_routes
|
41
|
-
"/#{
|
42
|
+
"/#{category_path}#{query}"
|
42
43
|
else
|
43
44
|
effective_posts.posts_path(opts.merge(category: category.presence).compact)
|
44
45
|
end
|
@@ -68,7 +69,7 @@ module EffectivePostsHelper
|
|
68
69
|
end
|
69
70
|
|
70
71
|
def admin_post_status_badge(post)
|
71
|
-
return nil unless
|
72
|
+
return nil unless EffectiveResources.authorized?(self, :admin, :effective_posts)
|
72
73
|
|
73
74
|
if post.draft?
|
74
75
|
content_tag(:span, 'DRAFT', class: 'badge badge-info')
|
@@ -77,29 +78,9 @@ module EffectivePostsHelper
|
|
77
78
|
end
|
78
79
|
end
|
79
80
|
|
80
|
-
# Only supported options are:
|
81
|
-
# :label => 'Read more' to set the label of the 'Read More' link
|
82
|
-
# :omission => '...' passed to the final text node's truncate
|
83
|
-
# :length => 200 to set the max inner_text length of the content
|
84
81
|
# All other options are passed to the link_to 'Read more'
|
85
|
-
def post_excerpt(post,
|
86
|
-
|
87
|
-
divider = content.index(Effective::Snippets::ReadMoreDivider::TOKEN)
|
88
|
-
excerpt = post.excerpt.to_s
|
89
|
-
|
90
|
-
read_more = (read_more_link && label.present?) ? readmore_link(post, label: label) : ''
|
91
|
-
|
92
|
-
html = (
|
93
|
-
if divider.present?
|
94
|
-
truncate_html(content, Effective::Snippets::ReadMoreDivider::TOKEN, '')
|
95
|
-
elsif length.present?
|
96
|
-
truncate_html(excerpt, length, omission)
|
97
|
-
else
|
98
|
-
excerpt
|
99
|
-
end
|
100
|
-
).html_safe
|
101
|
-
|
102
|
-
(html + read_more).html_safe
|
82
|
+
def post_excerpt(post, label: 'Continue reading')
|
83
|
+
(post.excerpt.to_s + readmore_link(post, label: label)).html_safe
|
103
84
|
end
|
104
85
|
|
105
86
|
def read_more_link(post, options = {})
|
@@ -10,7 +10,8 @@ module Effective
|
|
10
10
|
mail(
|
11
11
|
to: EffectivePosts.mailer[:admin_email],
|
12
12
|
from: EffectivePosts.mailer[:default_from],
|
13
|
-
subject: subject_for_post_submitted_to_admin(@post)
|
13
|
+
subject: subject_for_post_submitted_to_admin(@post),
|
14
|
+
tenant: (Tenant.current if defined?(Tenant))
|
14
15
|
)
|
15
16
|
end
|
16
17
|
|
@@ -1,55 +1,63 @@
|
|
1
1
|
module Effective
|
2
2
|
class Post < ActiveRecord::Base
|
3
|
-
|
3
|
+
attr_accessor :current_user
|
4
|
+
|
4
5
|
acts_as_slugged
|
5
6
|
|
6
|
-
if
|
7
|
-
|
8
|
-
end
|
7
|
+
log_changes if respond_to?(:log_changes)
|
8
|
+
acts_as_role_restricted if respond_to?(:acts_as_role_restricted)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
has_one_attached :image
|
11
|
+
|
12
|
+
has_rich_text :excerpt
|
13
|
+
has_rich_text :body
|
13
14
|
|
14
15
|
self.table_name = EffectivePosts.posts_table_name.to_s
|
15
16
|
|
16
|
-
belongs_to :user, optional: true
|
17
|
+
belongs_to :user, polymorphic: true, optional: true
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
effective_resource do
|
20
|
+
title :string
|
21
|
+
description :string
|
21
22
|
|
22
|
-
|
23
|
+
category :string
|
24
|
+
slug :string
|
23
25
|
|
24
|
-
|
26
|
+
draft :boolean
|
27
|
+
published_at :datetime
|
28
|
+
tags :text
|
25
29
|
|
26
|
-
|
27
|
-
# published_at :datetime
|
28
|
-
# tags :text
|
30
|
+
roles_mask :integer
|
29
31
|
|
30
|
-
|
32
|
+
# Event Fields
|
33
|
+
start_at :datetime
|
34
|
+
end_at :datetime
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
# location :string
|
36
|
-
# website_name :website_name
|
37
|
-
# website_href :website_href
|
36
|
+
location :string
|
37
|
+
website_name :string
|
38
|
+
website_href :string
|
38
39
|
|
39
|
-
|
40
|
+
timestamps
|
41
|
+
end
|
42
|
+
|
43
|
+
before_validation(if: -> { current_user.present? }) do
|
44
|
+
self.user ||= current_user
|
45
|
+
end
|
40
46
|
|
41
47
|
validates :title, presence: true, length: { maximum: 255 }
|
42
48
|
validates :description, presence: true, length: { maximum: 150 }
|
43
49
|
validates :category, presence: true
|
44
50
|
validates :published_at, presence: true
|
45
51
|
|
46
|
-
validates :start_at, presence: true, if: -> { category == 'events'}
|
52
|
+
validates :start_at, presence: true, if: -> { category == 'events' }
|
47
53
|
|
48
54
|
scope :drafts, -> { where(draft: true) }
|
49
55
|
scope :published, -> { where(draft: false).where("#{EffectivePosts.posts_table_name}.published_at < ?", Time.zone.now) }
|
50
56
|
scope :unpublished, -> { where(draft: true).or(where("#{EffectivePosts.posts_table_name}.published_at > ?", Time.zone.now)) }
|
51
57
|
scope :with_category, -> (category) { where(category: category.to_s.downcase) }
|
52
58
|
|
59
|
+
scope :deep, -> { with_rich_text_excerpt_and_embeds.with_rich_text_body_and_embeds }
|
60
|
+
|
53
61
|
scope :paginate, -> (page: nil, per_page: EffectivePosts.per_page) {
|
54
62
|
page = (page || 1).to_i
|
55
63
|
offset = [(page - 1), 0].max * per_page
|
@@ -58,7 +66,7 @@ module Effective
|
|
58
66
|
}
|
59
67
|
|
60
68
|
scope :posts, -> (user: nil, category: nil, unpublished: false) {
|
61
|
-
scope = all.
|
69
|
+
scope = all.deep.order(published_at: :desc)
|
62
70
|
|
63
71
|
if defined?(EffectiveRoles) && EffectivePosts.use_effective_roles
|
64
72
|
if user.present? && user.respond_to?(:roles)
|
@@ -77,16 +85,12 @@ module Effective
|
|
77
85
|
scope
|
78
86
|
}
|
79
87
|
|
80
|
-
def to_param
|
81
|
-
slug.presence || "#{id}-#{title.parameterize}"
|
82
|
-
end
|
83
|
-
|
84
88
|
def to_s
|
85
89
|
title.presence || 'New Post'
|
86
90
|
end
|
87
91
|
|
88
92
|
def published?
|
89
|
-
!draft? && published_at < Time.zone.now
|
93
|
+
!draft? && published_at.present? && published_at < Time.zone.now
|
90
94
|
end
|
91
95
|
|
92
96
|
def approved?
|
@@ -97,53 +101,34 @@ module Effective
|
|
97
101
|
category == 'events'
|
98
102
|
end
|
99
103
|
|
100
|
-
def body
|
101
|
-
region(:body).content
|
102
|
-
end
|
103
|
-
|
104
|
-
def body=(input)
|
105
|
-
region(:body).content = input
|
106
|
-
end
|
107
|
-
|
108
104
|
# 3.333 words/second is the default reading speed.
|
109
105
|
def time_to_read_in_seconds(reading_speed = 3.333)
|
110
106
|
(regions.to_a.sum { |region| (region.content || '').scan(/\w+/).size } / reading_speed).seconds
|
111
107
|
end
|
112
108
|
|
113
109
|
def send_post_submitted_to_admin!
|
114
|
-
|
110
|
+
deliver_method = EffectivePosts.mailer[:deliver_method] || raise('expected an EffectivePosts.deliver_method')
|
111
|
+
Effective::PostsMailer.post_submitted_to_admin(to_param).send(deliver_method)
|
115
112
|
end
|
116
113
|
|
117
114
|
# Returns a duplicated post object, or throws an exception
|
118
|
-
def duplicate
|
115
|
+
def duplicate
|
119
116
|
Post.new(attributes.except('id', 'updated_at', 'created_at')).tap do |post|
|
120
117
|
post.title = post.title + ' (Copy)'
|
121
118
|
post.slug = post.slug + '-copy'
|
122
119
|
post.draft = true
|
123
120
|
|
124
|
-
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
post.save!
|
121
|
+
post.body = body
|
122
|
+
post.excerpt = excerpt
|
129
123
|
end
|
130
124
|
end
|
131
125
|
|
132
|
-
|
126
|
+
def duplicate!
|
127
|
+
duplicate.tap { |post| post.save! }
|
128
|
+
end
|
133
129
|
|
134
|
-
def
|
135
|
-
|
136
|
-
if EffectivePosts.mailer[:delayed_job_deliver] && EffectivePosts.mailer[:deliver_method] == :deliver_later
|
137
|
-
Effective::PostsMailer.delay.public_send(email, *mailer_args)
|
138
|
-
elsif EffectivePosts.mailer[:deliver_method].present?
|
139
|
-
Effective::PostsMailer.public_send(email, *mailer_args).public_send(EffectivePosts.mailer[:deliver_method])
|
140
|
-
else
|
141
|
-
Effective::PostsMailer.public_send(email, *mailer_args).deliver_now
|
142
|
-
end
|
143
|
-
rescue => e
|
144
|
-
raise e unless Rails.env.production?
|
145
|
-
return false
|
146
|
-
end
|
130
|
+
def approve!
|
131
|
+
update!(draft: false)
|
147
132
|
end
|
148
133
|
|
149
134
|
end
|