Class: Pulitzer::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pulitzer/posts_controller.rb

Defined Under Namespace

Modules: Validations Classes: Create, Export, PreviewRebuilder, Update

Instance Method Summary collapse

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
# File 'app/controllers/pulitzer/posts_controller.rb', line 15

def create
  @post = Create.new(post_params).call
  if @post.errors.empty?
    Pulitzer::CreatePostContentElements.new(@post).call if @post
    render partial: 'show_wrapper', locals: { post: @post }
  else
    render partial: 'new', locals: { post: @post }, status: 409
  end
end

#destroyObject



61
62
63
64
# File 'app/controllers/pulitzer/posts_controller.rb', line 61

def destroy
  @post.destroy
  render head :ok and return
end

#editObject



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/pulitzer/posts_controller.rb', line 29

def edit
  route = "#{Pulitzer.preview_namespace}_#{@post.post_type.name.parameterize(separator: '_')}_path"
  if main_app.respond_to?(route)
    if @post.plural?
      @preview_path = main_app.public_send(route, @post.slug, {version_number: @post.post_type_version.version_number})
    else
      @preview_path = main_app.public_send(route, {version_number: @post.post_type_version.version_number})
    end
  end
  render_ajax locals: { post: @post }
end

#edit_slugObject



66
67
68
69
70
# File 'app/controllers/pulitzer/posts_controller.rb', line 66

def edit_slug
  if request.xhr?
    render partial: 'form_slug', locals: { post: @post }
  end
end

#edit_titleObject



41
42
43
# File 'app/controllers/pulitzer/posts_controller.rb', line 41

def edit_title
  render partial: 'form', locals: { post: @post }
end

#exportObject



45
46
47
48
49
50
# File 'app/controllers/pulitzer/posts_controller.rb', line 45

def export
  post_json = Export.new(@post).call
  send_data(post_json,
    disposition: 'attachment',
    filename: @post.title.parameterize + '_post.json')
end

#indexObject



4
5
6
7
8
# File 'app/controllers/pulitzer/posts_controller.rb', line 4

def index
  @post_type_version = Pulitzer::PostTypeVersion.find params[:post_type_version_id]
  @posts = Pulitzer::Post.where(post_type_version_id: @post_type_version).order(id: :desc)
  render_ajax
end

#newObject



10
11
12
13
# File 'app/controllers/pulitzer/posts_controller.rb', line 10

def new
  @post = Pulitzer::Post.new(post_type_version_id: params[:post_type_version_id])
  render_ajax locals: { post: @post }
end

#showObject



25
26
27
# File 'app/controllers/pulitzer/posts_controller.rb', line 25

def show
  render_ajax locals: { post: @post }
end

#show_slugObject



72
73
74
# File 'app/controllers/pulitzer/posts_controller.rb', line 72

def show_slug
  render partial: 'show_slug', locals: { post: @post, version: @version }
end

#updateObject



52
53
54
55
56
57
58
59
# File 'app/controllers/pulitzer/posts_controller.rb', line 52

def update
  Update.new(@post,post_params).call
  if @post.errors.empty?
    render partial: 'show', locals: { post: @post }
  else
    render partial: 'form', locals: { post: @post }, status: 409
  end
end

#update_slugObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/pulitzer/posts_controller.rb', line 76

def update_slug
  Update.new(@post,post_params).call
  if @post.errors.empty?
    route                       = "#{Pulitzer.preview_namespace}_#{@post.post_type.name.parameterize(separator: '_')}_path"
    if @post.plural?
      @preview_path = main_app.public_send(route, @post.slug, {version_number: @post.post_type_version.version_number})
    else
      @preview_path = main_app.public_send(route, {version_number: @post.post_type_version.version_number})
    end
    render partial: 'show_slug', locals: { post: @post }
  else
    render partial: 'form_slug', locals: { post: @post }, status: :conflict
  end
end