Skip to content

Instantly share code, notes, and snippets.

@hanachin
Forked from sinsoku/action.rb
Last active March 18, 2016 15:15
Show Gist options
  • Select an option

  • Save hanachin/b9bceb6f74ac3cb897b0 to your computer and use it in GitHub Desktop.

Select an option

Save hanachin/b9bceb6f74ac3cb897b0 to your computer and use it in GitHub Desktop.

Revisions

  1. hanachin revised this gist Mar 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post_actions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # app/controllers/post_actions_controller.rb
    class PostsActionsController < ApplicationController
    class PostActionsController < ApplicationController
    # POST /posts/:id/publish.json
    def publish
    if current_post.update(state: :publish, published_at: Time.zone.now)
  2. hanachin revised this gist Mar 18, 2016. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion actions_controller.rb → post_actions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # app/controllers/posts_actions_controller.rb
    # app/controllers/post_actions_controller.rb
    class PostsActionsController < ApplicationController
    # POST /posts/:id/publish.json
    def publish
    2 changes: 1 addition & 1 deletion routes.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    Rails.application.routes.draw do
    resources :posts do
    scope controller: :posts_actions do
    scope controller: :post_actions do
    member do
    post :publish
    post :hide
  3. hanachin revised this gist Mar 18, 2016. 3 changed files with 33 additions and 32 deletions.
    18 changes: 0 additions & 18 deletions action.rb
    Original file line number Diff line number Diff line change
    @@ -1,18 +0,0 @@
    # app/models/posts/action.rb
    class Action
    include ActiveModel::Model

    attr_reader :post_id, :name

    def save
    return false if invalid?

    @post = Post.find(post_id)
    if name == 'publish'
    @post.update state: :publish, published_at: Time.zone.now
    elsif name == 'xxx'
    # do something
    end
    # 適当にif分岐にしてるけど、本当はもう少しクラス設計を直して、if文がなくなるようにした方が良い
    end
    end
    37 changes: 23 additions & 14 deletions actions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,26 @@
    # app/controllers/posts/actions_controller.rb
    module Posts
    class ActionsController < ApplicationController
    # POST /posts/:post_id/actions.json
    def create
    @action = Posts::Action.new(post_id: params[:post_id], name: params[:name])

    respond_to do |format|
    if @action.save
    format.json { render json: @action, status: :created }
    else
    format.json { render json: @action.errors, status: :unprocessable_entity }
    else
    end
    # app/controllers/posts_actions_controller.rb
    class PostsActionsController < ApplicationController
    # POST /posts/:id/publish.json
    def publish
    if current_post.update(state: :publish, published_at: Time.zone.now)
    else
    end
    end

    # POST /posts/:id/hide.json
    def hide
    if current_post.update(state: :hide, published_at: nil)
    else
    end
    end

    private

    def current_post
    Post.find(params[:id])
    end
    end
    10 changes: 10 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    Rails.application.routes.draw do
    resources :posts do
    scope controller: :posts_actions do
    member do
    post :publish
    post :hide
    end
    end
    end
    end
  4. @sinsoku sinsoku revised this gist Mar 18, 2016. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion action.rb
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,11 @@ def save
    return false if invalid?

    @post = Post.find(post_id)
    # name の内容で処理を分岐したり、コールバックを書いたり。
    if name == 'publish'
    @post.update state: :publish, published_at: Time.zone.now
    elsif name == 'xxx'
    # do something
    end
    # 適当にif分岐にしてるけど、本当はもう少しクラス設計を直して、if文がなくなるようにした方が良い
    end
    end
  5. @sinsoku sinsoku created this gist Mar 18, 2016.
    13 changes: 13 additions & 0 deletions action.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # app/models/posts/action.rb
    class Action
    include ActiveModel::Model

    attr_reader :post_id, :name

    def save
    return false if invalid?

    @post = Post.find(post_id)
    # name の内容で処理を分岐したり、コールバックを書いたり。
    end
    end
    17 changes: 17 additions & 0 deletions actions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # app/controllers/posts/actions_controller.rb
    module Posts
    class ActionsController < ApplicationController
    # POST /posts/:post_id/actions.json
    def create
    @action = Posts::Action.new(post_id: params[:post_id], name: params[:name])

    respond_to do |format|
    if @action.save
    format.json { render json: @action, status: :created }
    else
    format.json { render json: @action.errors, status: :unprocessable_entity }
    else
    end
    end
    end
    end