Skip to content

Commit

Permalink
[NewAdmin BO] Simple candidacies management
Browse files Browse the repository at this point in the history
  • Loading branch information
Soulou committed Jun 13, 2013
1 parent c6eecfa commit 345ae29
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 22 deletions.
6 changes: 5 additions & 1 deletion app/assets/javascripts/new_admin/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require bootstrap-datepicker
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.fr
//= require bootstrap-datepicker/locales/bootstrap-datepicker.de
//= require bootstrap-datepicker/locales/bootstrap-datepicker.es
//= require bootstrap-datepicker/locales/bootstrap-datepicker.uk
//= require select2
//= require underscore
//= require_tree .
2 changes: 1 addition & 1 deletion app/assets/javascripts/new_admin/candidacies.js.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$ ->
formatOptionCandidate = (candidate) ->
'<img src="https://voxe.s3.amazonaws.com/candidates/' + candidate.text.toLowerCase().replace(/\s+/, "-") + '-50.jpg">&nbsp;' + candidate.text
'<img src="https://voxe.s3.amazonaws.com/candidates/' + candidate.text.replace(/\s+/, "-") + '-50.jpg">&nbsp;' + candidate.text
$("#candidacy_candidates").select2
width: "250px"
formatResult: formatOptionCandidate
Expand Down
41 changes: 33 additions & 8 deletions app/assets/stylesheets/new_admin/candidacies.css.sass
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
ul.candidates
list-style-type: none
li.candidate
$candidacy_height: 50px
$candidacy_width: 250px
$candidacy_link_width: 220px
$candidacy_delete_padding: ($candidacy_width - $candidacy_link_width) / 2
$candidacy_border_size: 1px
$candidacy_border_color: #c2c2c2
$candidacy_published_color: #DCF7E3
$candidacy_unpublished_color: #F2D3D3


.candidacy
border: $candidacy_border_size solid $candidacy_border_color
display: inline-block
width: $candidacy_width
a
display: inline-block
background-color: #e0e0e0
height: 50px
width: 250px
margin-bottom: 5px
border: 1px solid #c2c2c2
height: $candidacy_height
width: $candidacy_link_width
vertical-align: middle
&.candidacy-published
background-color: $candidacy_published_color
&.candidacy-unpublished
background-color: $candidacy_unpublished_color
&.candidacy-delete
border-left: $candidacy_border_size solid darken($candidacy_border_color, 20%)
float: right
text-align: center
height: 100%
padding-top: $candidacy_delete_padding
padding-bottom: $candidacy_delete_padding
font-size: 40px
color: darken($candidacy_unpublished_color, 40%)
width: $candidacy_width - $candidacy_link_width - $candidacy_border_size
background-color: darken($candidacy_unpublished_color, 20%)

#s2id_candidacy_candidates .select2-choice
height: 50px
51 changes: 47 additions & 4 deletions app/controllers/new_admin/candidacies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
class NewAdmin::CandidaciesController < AdminController
def index
@election = Election.find params[:election_id]
@candidates = @election.candidacies.map{|c| c.candidates[0]}.sort do |c1,c2|
c1.last_name <=> c2.last_name
end
load_candidates
@candidacy ||= Candidacy.new
@available_candidates = Candidate.all.to_a.select do |c| !@candidates.include? c end
end

def create
@election = Election.find params[:election_id]

if params[:candidacy][:candidates].empty?
flash[:error] = "Please choose a candidate to add"
return respond_with nil, location: new_admin_election_candidacies_path(@election)
end

candidate = Candidate.find params[:candidacy][:candidates]

@candidacy = Candidacy.new election: @election, candidates: [candidate]

if @candidacy.save
flash[:notice] = "#{candidate} attends to the election : #{@election}"
respond_with @candidacy, location: new_admin_election_candidacies_path(@election)
else
flash[:error] = "Fail to add #{candidate} to #{@election}"
render :index
end
end

def destroy
candidacy = Candidacy.find params[:id]
candidacy.destroy
@election = Election.find params[:election_id]
respond_with nil, location: new_admin_election_candidacies_path(@election)
end

def toggle
candidacy = Candidacy.find params[:candidacy_id]
candidacy.published = !candidacy.published
candidacy.save
@election = Election.find params[:election_id]
respond_with nil, location: new_admin_election_candidacies_path(@election)
end

protected

def load_candidates
@candidacies = @election.candidacies.to_a.sort do |c1,c2|
c1.candidates[0].last_name <=> c2.candidates[0].last_name
end
candidates = @candidacies.map{ |c| c.candidates[0]}
@available_candidates = Candidate.all.to_a.select do |c| !candidates.include? c end
end
end
3 changes: 3 additions & 0 deletions app/models/candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def photo_url(size = nil)
"https://voxe.s3.amazonaws.com/candidates/#{namespace}-#{width}.jpg"
end

def to_s
"#{name}"
end
private

def default_photo(size)
Expand Down
7 changes: 7 additions & 0 deletions app/views/new_admin/candidacies/_candidacy.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.candidacy
= link_to "×", new_admin_election_candidacy_path(@election, candidacy), class: "candidacy-delete", method: "DELETE"

= link_to new_admin_election_candidacy_toggle_path(@election, candidacy), class: "candidacy-#{candidacy_state}", method: "POST" do
= image_tag candidacy.candidates[0].photo_url(:small)
= candidacy.candidates[0]

4 changes: 4 additions & 0 deletions app/views/new_admin/candidacies/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
= simple_form_for [:new_admin, @election, @candidacy], html: {class: "form-inline"} do |f|
= f.label :candidates, "Candidate : "
= f.input_field :candidates, collection: @available_candidates
= f.submit "Add"
17 changes: 10 additions & 7 deletions app/views/new_admin/candidacies/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
= simple_form_for [:new_admin, @election, @candidacy] do |f|
= f.input :candidates, collection: @available_candidates
= render '/new_admin/flash', flash: flash

%ul.candidates
- @candidates.each do |candidate|
%li.candidate
= image_tag candidate.photo_url(:small)
= candidate.name
= render 'form'

.candidates
- @candidacies.each do |candidacy|
- candidate = candidacy.candidates[0]
- if candidacy.published
= render "candidacy", candidacy: candidacy, candidacy_state: "published"
- else
= render "candidacy", candidacy: candidacy, candidacy_state: "unpublished"

4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
resources :tags, only: [:create]
resources :elections do
resources :election_tags, only: [:index]
resources :candidacies, only: [:index]
resources :candidacies, only: [:index, :create, :destroy] do
post :toggle
end
post 'publish'
post 'unpublish'
resources :tags, only: [:create]
Expand Down

0 comments on commit 345ae29

Please sign in to comment.