diff --git a/app/controllers/changes.rb b/app/controllers/changes.rb index 577688a..3b9fcca 100644 --- a/app/controllers/changes.rb +++ b/app/controllers/changes.rb @@ -4,7 +4,6 @@ if is_logged_in? @badge = Badge.find params[:id] @change = BadgeChange.new badge: @badge, user: current_user - @change.votes.build render 'new' else redirect url(:static, :index) @@ -27,9 +26,9 @@ end end - put :accept, map: '/badges/:badge_id/change/:change_id/accept' do + put :accept, map: '/changes/:change_id/accept' do end - put :reject, map: '/badges/:badge_id/change/:change_id/reject' do + put :reject, map: '/changes/:change_id/reject' do end end diff --git a/app/controllers/votes.rb b/app/controllers/votes.rb new file mode 100644 index 0000000..f013440 --- /dev/null +++ b/app/controllers/votes.rb @@ -0,0 +1,28 @@ +Embadge::App.controllers :votes do + + get :new, map: '/changes/:id/vote/new' do + @change = BadgeChange.find params[:id] + if is_logged_in? + @vote = Vote.new badge_change: @change, user: current_user + render 'new' + else + redirect url(:static, :index) + end + end + + post :create, map: '/changes/:id/vote/create' do + if is_logged_in? + @vote = Vote.new(params[:vote]) + @change = BadgeChange.find params[:id] + @vote.badge_change = @change + @vote.user = current_user + + if @vote.save + redirect(url(:badges, :show, id: @change.badge.id)) + else + flash.now[:error] = "Nope" + render 'votes/new' + end + end + end +end diff --git a/app/helpers/auth_helper.rb b/app/helpers/auth_helper.rb index 4d3e867..94839fa 100644 --- a/app/helpers/auth_helper.rb +++ b/app/helpers/auth_helper.rb @@ -7,10 +7,6 @@ def current_user @current_user ||= User.find_by_id(session[:current_user]) end - def reset_session - - end - def is_logged_in? return current_user.present? end diff --git a/app/helpers/votes_helper.rb b/app/helpers/votes_helper.rb new file mode 100644 index 0000000..6c347da --- /dev/null +++ b/app/helpers/votes_helper.rb @@ -0,0 +1,13 @@ +# Helper methods defined here can be accessed in any controller or view in the application + +module Embadge + class App + module VotesHelper + # def simple_helper_method + # ... + # end + end + + helpers VotesHelper + end +end diff --git a/app/views/votes/_form.haml b/app/views/votes/_form.haml new file mode 100644 index 0000000..2218528 --- /dev/null +++ b/app/views/votes/_form.haml @@ -0,0 +1,7 @@ += f.error_messages +%div.form-group + = f.label :comment + = f.text_area :comment, class: 'form-control' + +%div.form-group + = f.submit "Submit", class: 'btn btn-default' diff --git a/app/views/votes/new.haml b/app/views/votes/new.haml new file mode 100644 index 0000000..c68bc3b --- /dev/null +++ b/app/views/votes/new.haml @@ -0,0 +1,6 @@ +%h2 Vote += flash_tag(:error) += form_for @vote, url(:votes, :create, id: @change.id), id: 'new_vote' do |f| + = partial 'votes/form', locals: {f: f} + +%a{href: url(:badges, :show, id: @change.badge.id)} Back to badge