Class: Chive::ArticlesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/chive/articles_controller.rb

Instance Method Summary collapse

Methods included from UserConcerns

#authenticate_chive_user, #chive_user, #user_can_chive?

Instance Method Details

#createObject

POST /articles POST /articles.json



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/chive/articles_controller.rb', line 38

def create
  @article = Article.new(article_params)
  @article.author = chive_user

  respond_to do |format|
    if @article.save
      format.html { redirect_to @article, notice: 'Article was successfully created.' }
      format.json { render :show, status: :created, location: @article }
    else
      format.html { render :new }
      format.json { render json: @article.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /articles/1 DELETE /articles/1.json



72
73
74
75
76
77
78
# File 'app/controllers/chive/articles_controller.rb', line 72

def destroy
  @article.destroy
  respond_to do |format|
    format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#editObject

GET /articles/1/edit



33
34
# File 'app/controllers/chive/articles_controller.rb', line 33

def edit
end

#indexObject

GET /articles GET /articles.json



13
14
15
16
17
18
19
# File 'app/controllers/chive/articles_controller.rb', line 13

def index
  @articles = if user_can_chive? && !params.key?(:public)
    Article.latest
  else
    Article.latest_published
  end.paginate(page: params[:page], per_page: Chive.per_page)
end

#newObject

GET /articles/new



28
29
30
# File 'app/controllers/chive/articles_controller.rb', line 28

def new
  @article = Article.new(published_at: DateTime.now)
end

#showObject

GET /articles/1 GET /articles/1.json

Raises:

  • (ActionController::RoutingError)


23
24
25
# File 'app/controllers/chive/articles_controller.rb', line 23

def show
  raise ActionController::RoutingError.new('Not Found') unless @article
end

#updateObject

PATCH/PUT /articles/1 PATCH/PUT /articles/1.json



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/chive/articles_controller.rb', line 55

def update
  respond_to do |format|
    if params[:delete_image] || params[:article][:image]
      @article.image.purge
    end
    if @article.update(article_params)
      format.html { redirect_to @article, notice: 'Article was successfully updated.' }
      format.json { render :show, status: :ok, location: @article }
    else
      format.html { render :edit }
      format.json { render json: @article.errors, status: :unprocessable_entity }
    end
  end
end