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 inherited from Virgo::ApplicationController

#sitemap

Methods included from RenderHelper

#render_content, #with_format

Methods included from Virgo::ApplicationHelper

#action?, #admin?, #admin_access?, #admin_view?, #alerts, #base_errors, #category_id_param, #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?, #tab_param, #tabbed_param, #word_count

Instance Method Details

#author_dropdownObject



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

def author_dropdown
  @post = Post.find_by(id: post_id_param)

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

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/virgo/admin/posts_controller.rb', line 42

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


113
114
115
116
117
# File 'app/controllers/virgo/admin/posts_controller.rb', line 113

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

  head :ok
end

#delete_thumbnail_imageObject



119
120
121
122
123
# File 'app/controllers/virgo/admin/posts_controller.rb', line 119

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

  head :ok
end

#destroyObject



90
91
92
93
94
95
96
97
98
# File 'app/controllers/virgo/admin/posts_controller.rb', line 90

def destroy
  authorize! :destroy, @post

  @post.destroy

  flash[:notice] = "Post successfully deleted"

  redirect_to admin_posts_path
end

#editObject



58
59
60
# File 'app/controllers/virgo/admin/posts_controller.rb', line 58

def edit
  @post.attempt_edit_lock(current_user)
end

#editingObject



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

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



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

def find
  @posts = Post.search_by_similarity(term_param)

  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 sort_param.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(page_param)
end

#newObject



30
31
32
# File 'app/controllers/virgo/admin/posts_controller.rb', line 30

def new
  @post = Post.new(post_params.merge(author: current_user))
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.



128
129
130
131
132
# File 'app/controllers/virgo/admin/posts_controller.rb', line 128

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

  Rails.cache.clear
end

#revision_detailObject



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

def revision_detail
  @version = PaperTrail::Version.find(version_id_param)
  @post = @version.item
end

#revisionsObject



39
40
# File 'app/controllers/virgo/admin/posts_controller.rb', line 39

def revisions
end

#showObject



55
56
# File 'app/controllers/virgo/admin/posts_controller.rb', line 55

def show
end

#updateObject



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

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