Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes user update #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Admin::UsersController < Admin::BaseController
def index
@users = User.unscoped.all

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body end.

end

def show
Expand All @@ -26,9 +27,23 @@ def update

def update_role
@user = User.find(params[:user_id])
@user.update(user_role_params)
flash[:success] = "The user role has been updated to #{@user.role}."
redirect_to admin_dash_users_path
@merchant = Merchant.where(name: params[:user][:merchant]).first
if params[:user][:role] == "merchant_employee" && @merchant
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationConsistency: Inconsistent indentation detected.
Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

@user.update(user_role_params)
@user.merchant_id = @merchant.id
@user.save
elsif params[:user][:role] != "merchant_employee" && @user.merchant_id != nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Style/NonNilCheck: Prefer !expression.nil? over expression != nil.
Metrics/LineLength: Line is too long. [83/80]

@user.update(user_role_params)
@user.merchant_id = nil
@user.save
end
if @user.update
flash[:success] = "The user role has been updated to #{@user.role}."
redirect_to admin_dash_users_path
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IdenticalConditionalBranches: Move redirect_to admin_dash_users_path out of the conditional.

else
flash[:error] = "Please try again. You may need to choose a merchant."
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

redirect_to admin_dash_users_path
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/IdenticalConditionalBranches: Move redirect_to admin_dash_users_path out of the conditional.

end
end

def toggle_active
Expand Down
6 changes: 5 additions & 1 deletion app/views/admin/users/_role.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<center>
<%= form_for [:admin, user], url: "/admin/users/#{user.id}/role", :html => { :style => 'background-color: #17BEBB;' } do |f| %>
<%= f.label :role%>
<%= f.select :role, ['user', 'merchant_employee', 'merchant_admin', 'admin_user']%>
<%= f.select :role, ['user', 'merchant_employee', 'admin_user']%>

<%= f.label :merchant %>
<%= f.select :merchant, Merchant.all.map {|merchant| [merchant.name]}, :prompt => 'Select a Merchant'%>

<%= f.submit %>
<% end %>
</center>
52 changes: 49 additions & 3 deletions spec/features/admin/user/updates_role_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
user = create(:random_user)
user_2 = create(:random_user)
user_3 = create(:random_user)

merchant = create(:ray_merchant)
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin)

visit admin_dash_users_path
Expand All @@ -15,12 +15,58 @@

within "#user-#{user.id}" do
expect(page).to have_content('Role')
select "merchant_admin", :from => "Role"
select "merchant_employee", :from => "Role"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

select("Vintage Vampire", from: "Merchant").select_option
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

click_on "Update"
end

user.reload
expect(user.role).to eq("merchant_employee")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

expect(user.merchant_id).to eq(merchant.id)
expect(page).to have_content("The user role has been updated to #{user.role}.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [83/80]

end

it "won't update a merchant id if its not a merchant employee" do
admin = create(:admin_user)
user = create(:merchant_employee)
merchant = create(:ray_merchant)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UselessAssignment: Useless assignment to variable - merchant.

allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [92/80]


visit admin_dash_users_path

expect(user.role).to eq("merchant_employee")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


within "#user-#{user.id}" do
select "user", :from => "Role"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

select("Vintage Vampire", from: "Merchant").select_option
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

click_on "Update"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end

user.reload
expect(user.role).to eq("merchant_admin")
expect(user.role).to eq("user")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

expect(user.merchant_id).to eq(nil)
expect(page).to have_content("The user role has been updated to #{user.role}.")
end

it "won't update if no merchant is given for a merchant employee" do
admin = create(:admin_user)
user = create(:random_user)
merchant = create(:ray_merchant)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UselessAssignment: Useless assignment to variable - merchant.

allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(admin)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [92/80]


visit admin_dash_users_path

expect(user.role).to eq("user")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


within "#user-#{user.id}" do
select "merchant_employee", :from => "Role"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

click_on "Update"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

end

user.reload
expect(user.role).to eq("user")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

expect(user.merchant_id).to eq(nil)
save_and_open_page
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationConsistency: Inconsistent indentation detected.
Lint/Debugger: Remove debugger entry point save_and_open_page.

expect(page).to have_content("Please try again. You may need to choose a merchant.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Metrics/LineLength: Line is too long. [88/80]

end
end