Class: Effective::PostsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/effective/posts_controller.rb', line 66

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

  EffectiveResources.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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/effective/posts_controller.rb', line 119

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

  EffectiveResources.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



89
90
91
92
93
94
# File 'app/controllers/effective/posts_controller.rb', line 89

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

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

#indexObject



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

def index
  @posts ||= Effective::Post.posts(
    user: current_user,
    category: params[:category],
    unpublished: EffectiveResources.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

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

  @page_title ||= [(params[:category] || 'Blog').to_s.titleize, (" - Page #{params[:page]}" if params[:page])].compact.join
  @canonical_url ||= helpers.(params[:category], page: params[:page])
end

#newObject

Public user submit a post functionality



59
60
61
62
63
64
# File 'app/controllers/effective/posts_controller.rb', line 59

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

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

#showObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/effective/posts_controller.rb', line 34

def show
  @posts ||= Effective::Post.posts(user: current_user, category: params[:category], unpublished: EffectiveResources.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

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

  if EffectiveResources.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_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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/effective/posts_controller.rb', line 96

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

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

  if @post.update(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