Skip to content

Instantly share code, notes, and snippets.

@abhchand
Last active September 12, 2017 13:20
Show Gist options
  • Select an option

  • Save abhchand/883015f2e39639158ab7dd56b7030b55 to your computer and use it in GitHub Desktop.

Select an option

Save abhchand/883015f2e39639158ab7dd56b7030b55 to your computer and use it in GitHub Desktop.

Revisions

  1. abhchand revised this gist Sep 10, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion users_create_action_interactor.rb
    Original file line number Diff line number Diff line change
    @@ -56,6 +56,6 @@ def handle_email_format_invalid
    end

    def handle_success
    # tbd
    # n/a
    end
    end
  2. abhchand revised this gist Sep 10, 2017. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions users_controller_1.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    class UsersController < ApplicationController
    def create
    # Validate the form data and return if it is not correct
    unless create_action_validation.success?
    flash[:error] = create_action_validation.err_msg
    validator = UsersCreateActionInteractor.call(params: user_params)

    unless validator.success?
    flash[:error] = validator.err_msg
    redirect_to(new_users_path)
    return
    end
    @@ -22,9 +23,4 @@ def user_params
    :email
    )
    end

    def create_action_validation
    @create_action_validation || =
    UsersCreateActionInteractor.call(params: user_params)
    end
    end
  3. abhchand revised this gist Sep 10, 2017. 2 changed files with 41 additions and 5 deletions.
    30 changes: 30 additions & 0 deletions users_controller_1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    class UsersController < ApplicationController
    def create
    # Validate the form data and return if it is not correct
    unless create_action_validation.success?
    flash[:error] = create_action_validation.err_msg
    redirect_to(new_users_path)
    return
    end

    user = User.create!(user_params)
    flash[:success] = "Woohoo, you created an account"

    redirect_to(user_path(@ser))
    end

    private

    def user_params
    @user_params || = params.require(:user).permit(
    :first_name,
    :last_name,
    :email
    )
    end

    def create_action_validation
    @create_action_validation || =
    UsersCreateActionInteractor.call(params: user_params)
    end
    end
    16 changes: 11 additions & 5 deletions users_create_action_interactor.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    # app/interactors/users_create_action_interactor.rb
    class UsersCreateActionInteractor
    include Interactor

    @@ -22,31 +21,38 @@ def name_is_blank?
    end

    def handle_blank_name
    context.fail!(err_msg: "Please fill in a name")
    # Note: The interactor gem supports a short-hand notation for
    # the below actions:
    # > context.fail!(err_msg: "...")
    context.err_msg = "Please fill in a name"
    context.fail!
    end

    def email_is_blank?
    @params[:email].blank?
    end

    def handle_blank_email
    context.fail!(err_msg: "Please fill in an email")
    context.err_msg = "Please fill in an email"
    context.fail!
    end

    def email_is_taken?
    User.find_by_email(@params[:email]).any?
    end

    def handle_email_is_taken
    context.fail!(err_msg: "That email already exists!")
    context.err_msg = "That email already exists!"
    context.fail!
    end

    def email_format_invalid?
    (@params[:email] =~ /.*@.*\..*/i).blank?
    end

    def handle_email_format_invalid
    context.fail!(err_msg: "Sorry, that doesn't look like an email address")
    context.err_msg = "Sorry, that doesn't look like an email address"
    context.fail!
    end

    def handle_success
  4. abhchand revised this gist Sep 10, 2017. 1 changed file with 52 additions and 1 deletion.
    53 changes: 52 additions & 1 deletion users_create_action_interactor.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,55 @@
    # app/interactors/users_create_action_interactor.rb
    class UsersCreateActionInteractor
    include Interactor
    end

    def call
    @params = context.params

    case
    when name_is_blank? then handle_blank_name
    when email_is_blank? then handle_blank_email
    when email_is_taken? then handle_email_is_taken
    when email_format_invalid? then handle_email_format_invalid
    else
    handle_success
    end
    end

    private

    def name_is_blank?
    @params[:first_name].blank? || @params[:last_name].blank?
    end

    def handle_blank_name
    context.fail!(err_msg: "Please fill in a name")
    end

    def email_is_blank?
    @params[:email].blank?
    end

    def handle_blank_email
    context.fail!(err_msg: "Please fill in an email")
    end

    def email_is_taken?
    User.find_by_email(@params[:email]).any?
    end

    def handle_email_is_taken
    context.fail!(err_msg: "That email already exists!")
    end

    def email_format_invalid?
    (@params[:email] =~ /.*@.*\..*/i).blank?
    end

    def handle_email_format_invalid
    context.fail!(err_msg: "Sorry, that doesn't look like an email address")
    end

    def handle_success
    # tbd
    end
    end
  5. abhchand revised this gist Sep 10, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions users_create_action_interactor.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # app/interactors/users_create_action_interactor.rb
    class UsersCreateActionInteractor
    include Interactor
    end
  6. abhchand revised this gist Sep 9, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions users_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    class UsersController < ApplicationController
    def create
    # Validate the form data and return if
    # it is not correct
    # Validate the form data and return if it is not correct
    # We'll come back and fill in the ???? with the call to the
    # interactor shortly.
    unless (????)
    flash[:error] = ????
    redirect_to(new_users_path)
  7. abhchand created this gist Sep 9, 2017.
    26 changes: 26 additions & 0 deletions users_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    class UsersController < ApplicationController
    def create
    # Validate the form data and return if
    # it is not correct
    unless (????)
    flash[:error] = ????
    redirect_to(new_users_path)
    return
    end

    user = User.create!(user_params)
    flash[:success] = "Woohoo, you created an account"

    redirect_to(user_path(@ser))
    end

    private

    def user_params
    @user_params || = params.require(:user).permit(
    :first_name,
    :last_name,
    :email
    )
    end
    end