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



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

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



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

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



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

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
# 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

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

  @page_title ||= [(@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



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

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

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

#showObject



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

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

  @posts ||= Effective::Post.posts(user: current_user, 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



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

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