Class: Virgo::Admin::PostsController

Inherits:
BaseController show all
Defined in:
app/controllers/virgo/admin/posts_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#authorize_admin_user, #search, #set_is_admin_view

Methods included from RenderHelper

#render_content, #with_format

Methods included from Virgo::ApplicationHelper

#action?, #admin?, #admin_access?, #admin_view?, #alerts, #base_errors, #category_timestamp, #column_timestamp, #compact_html, #controller?, #decode_html_entities, #expanded_post_url, #is_admin_view?, #page_url, #post_time_format, #post_timestamp, #production?, #redis_timestamp_key_for, #site, #site_key, #superuser?, #tabbed_param, #word_count

Instance Method Details

#author_dropdownObject


74
75
76
77
78
79
80
# File 'app/controllers/virgo/admin/posts_controller.rb', line 74

def author_dropdown
  @post = Post.find_by(id: params[:post_id])

  render json: {
    html: render_content(partial: "author_dropdown")
  }
end

#createObject


54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/virgo/admin/posts_controller.rb', line 54

def create
  @post = Post.new(post_params.merge(created_by: current_user))

  authorize! :manage, @post

  if @post.save
    flash[:notice] = "#{model_name} successfully created. You can edit it below."
    redirect_to edit_admin_post_path(@post)
  else
    render 'new'
  end
end

125
126
127
128
129
# File 'app/controllers/virgo/admin/posts_controller.rb', line 125

def delete_featured_image
  @post.update!(featured_image_id: nil)

  head :ok
end

#delete_thumbnail_imageObject


131
132
133
134
135
# File 'app/controllers/virgo/admin/posts_controller.rb', line 131

def delete_thumbnail_image
  @post.update!(thumbnail_image_id: nil)

  head :ok
end

#destroyObject


102
103
104
105
106
107
108
109
110
# File 'app/controllers/virgo/admin/posts_controller.rb', line 102

def destroy
  authorize! :destroy, @post

  @post.destroy

  flash[:notice] = "Post successfully deleted"

  redirect_to admin_posts_path
end

#editObject


70
71
72
# File 'app/controllers/virgo/admin/posts_controller.rb', line 70

def edit
  @post.attempt_edit_lock(current_user)
end

#editingObject


112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/virgo/admin/posts_controller.rb', line 112

def editing
  if request.post?
    result = @post.attempt_edit_lock(current_user)

    render json: {
      edit_lock_succeeded: result,
      post_is_mid_edit: @post.is_mid_edit?,
      editor_is_self: (@post.editing_user == current_user),
      editor_byline: @post.editing_user.pretty_name
    }
  end
end

#findObject

auto-complete endpoint


34
35
36
37
38
39
40
# File 'app/controllers/virgo/admin/posts_controller.rb', line 34

def find
  @posts = Post.search_by_similarity(params[:term])

  render json: {
    posts: @posts
  }
end

#indexObject


9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/virgo/admin/posts_controller.rb', line 9

def index
  if params[:sort].blank?
    flash.keep
    redirect_to admin_posts_path(filters: filter_params, sort: '-publish_at') and return
  end

  @posts = Post.with_relations
               .search(filter_params)
               .order(sort_order)
               .page(params[:page])
end

#newObject


42
43
44
# File 'app/controllers/virgo/admin/posts_controller.rb', line 42

def new
  @post = Post.new(post_params.merge(author: current_user))
end

#optionsObject


21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/virgo/admin/posts_controller.rb', line 21

def options
  @posts = Post.search_by_headline(params[:term])

  data = @posts.map { |post|
    {value: post.id, label: truncate(post.headline, length: 100)}
  }

  render json: {
    posts: data
  }
end

#publish_immediatelyObject

Use with care (and perhaps only expose to admins): In certain circumstances, such as when chasing breaking news, we need to make immediate changes.


140
141
142
143
144
# File 'app/controllers/virgo/admin/posts_controller.rb', line 140

def publish_immediately
  @post.update!(publish_at: Time.now, live: true)

  Rails.cache.clear
end

#revision_detailObject


46
47
48
49
# File 'app/controllers/virgo/admin/posts_controller.rb', line 46

def revision_detail
  @version = PaperTrail::Version.find(params[:version_id])
  @post = @version.item
end

#revisionsObject


51
52
# File 'app/controllers/virgo/admin/posts_controller.rb', line 51

def revisions
end

#showObject


67
68
# File 'app/controllers/virgo/admin/posts_controller.rb', line 67

def show
end

#updateObject


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/virgo/admin/posts_controller.rb', line 82

def update
  @post.attributes = post_params

  authorize! :manage, @post

  if Rails.application.config.post_locking_enabled
    if @post.is_mid_edit? && @post.editing_user && @post.editing_user != current_user
      flash.now[:warning] = "Could not save changes because post is currently being edited by #{@post.editing_user.pretty_name}"
      render and return
    end
  end

  if @post.save
    flash[:notice] = "Post saved"
    redirect_to edit_admin_post_path(@post)
  else
    render 'new'
  end
end