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



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

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



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

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



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

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
33
34
35
36
37
38
39
40
# File 'app/controllers/effective/posts_controller.rb', line 10

def index
  @category = EffectivePosts.category(params[:category])

  @posts ||= Effective::Post.posts(
    user: current_user,
    category: @category,
    unpublished: EffectiveResources.authorized?(self, :admin, :effective_posts)
  )

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

  if EffectivePosts.event_categories.include?(@category)
    @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

  if @category.present?
    @page_title = @category.to_s
  else
    @page_title ||= view_context.posts_name_label
  end

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

  @page_title ||= [(@category || view_context.posts_name_label).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



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

def new
  @post ||= Effective::Post.new
  @page_title = 'New Post'

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

#showObject



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

def show
  @category = EffectivePosts.category(params[:category])

  admin = EffectiveResources.authorized?(self, :admin, :effective_posts)

  @posts ||= Effective::Post.posts(user: current_user, unpublished: admin, archived: admin)
  @post = @posts.find(params[:id])

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

  if admin 
    flash.now[:warning] = [
      'Hi Admin!',
      ('You are viewing a hidden post.' unless @post.published?),
      ('You are viewing an archived post.' if @post.archived?),
      ("<a href='#{effective_posts.edit_admin_post_path(@post)}' class='alert-link'>Edit this post</a>.")
    ].compact.join(' ')
  end

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

#updateObject



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

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