Class: Effective::PostsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/effective/posts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/effective/posts_controller.rb', line 72

def create
  @post ||= Effective::Post.new(post_params)
  @post.user = current_user if defined?(current_user)
  @post.draft = (EffectivePosts.submissions_require_approval == true)

  EffectivePosts.authorize!(self, :create, @post)

  if @post.save
    @page_title ||= 'Post Submitted'
    flash.now[:success] = 'Successfully submitted post'

    if EffectivePosts.submissions_require_approval
      @post.
    end

    render :submitted
  else
    @page_title ||= 'New Post'
    flash.now[:danger] = 'Unable to submit post'
    render action: :new
  end
end

#destroyObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/effective/posts_controller.rb', line 125

def destroy
  @post ||= Effective::Post.find(params[:id])

  EffectivePosts.authorize!(self, :destroy, @post)

  if @post.destroy
    flash[:success] = 'Successfully deleted post'
  else
    flash[:danger] = 'Unable to delete post'
  end

  redirect_to effective_posts.posts_path
end

#editObject



95
96
97
98
99
100
# File 'app/controllers/effective/posts_controller.rb', line 95

def edit
  @post ||= Effective::Post.find(params[:id])
  @page_title ||= 'Edit Post'

  EffectivePosts.authorize!(self, :edit, @post)
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/effective/posts_controller.rb', line 8

def index
  @posts ||= Effective::Post.posts(
    user: current_user,
    category: params[:category],
    unpublished: EffectivePosts.authorized?(self, :admin, :effective_posts)
  )

  @posts = @posts.paginate(page: params[:page])

  if params[:category] == 'events'
    @posts = @posts.reorder(:start_at).where('start_at > ?', Time.zone.now)
  end

  if params[:search].present?
    search = params[:search].permit(EffectivePosts.permitted_params).delete_if { |k, v| v.blank? }
    @posts = @posts.where(search) if search.present?
  end

  EffectivePosts.authorize!(self, :index, Effective::Post)

  if params[:page]
    @page_title ||= ((params[:category].presence || 'Blog').titleize + " - Page #{params[:page].to_s}")
    @canonical_url ||= effective_posts.posts_url(page: params[:page])
  else
    @page_title ||= (params[:category].presence || 'Blog').titleize
    @canonical_url ||= effective_posts.posts_url
  end

end

#newObject

Public user submit a post functionality



65
66
67
68
69
70
# File 'app/controllers/effective/posts_controller.rb', line 65

def new
  @post ||= Effective::Post.new(published_at: Time.zone.now)
  @page_title = 'New Post'

  EffectivePosts.authorize!(self, :new, @post)
end

#showObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/effective/posts_controller.rb', line 38

def show
  @posts ||= Effective::Post.posts(user: current_user, category: params[:category], unpublished: EffectivePosts.authorized?(self, :admin, :effective_posts))
  @post = @posts.find(params[:id])


  if @post.respond_to?(:roles_permit?)
    raise Effective::AccessDenied.new('Access Denied', :show, @post) unless @post.roles_permit?(current_user)
  end

  EffectivePosts.authorize!(self, :show, @post)

  if EffectivePosts.authorized?(self, :admin, :effective_posts)
    flash.now[:warning] = [
      'Hi Admin!',
      ('You are viewing a hidden post.' unless @post.published?),
      'Click here to',
      ("<a href='#{effective_regions.edit_path(effective_posts.post_path(@post, exit: effective_posts.post_path(@post)))}' class='alert-link'>edit post content</a> or" unless admin_edit?),
      ("<a href='#{effective_posts.edit_admin_post_path(@post)}' class='alert-link'>edit post settings</a>.")
    ].compact.join(' ')
  end

  @page_title ||= @post.title
  @meta_description ||= @post.description
  @canonical_url ||= effective_posts.post_url(@post)
end

#updateObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/effective/posts_controller.rb', line 102

def update
  @post ||= Effective::Post.find(params[:id])
  draft_was = @post.draft
  @post.draft = (EffectivePosts.submissions_require_approval == true)

  EffectivePosts.authorize!(self, :update, @post)

  if @post.update_attributes(post_params)
    @page_title ||= 'Post Submitted'
    flash.now[:success] = 'Successfully re-submitted post'

    if EffectivePosts.submissions_require_approval && draft_was != true
      @post.
    end

    render :submitted
  else
    @page_title ||= 'Edit Post'
    flash.now[:danger] = 'Unable to update post'
    render action: :edit
  end
end