Skip to content

Commit

Permalink
Shuffle helper modules into their own files.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanfarrar committed Sep 5, 2010
1 parent f889ae3 commit d2734fe
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 357 deletions.
16 changes: 0 additions & 16 deletions lib/controllers/config_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# for some reason, when a scrollable slot has native controls,
# the native controls render even when they should be scrolled out of view.

module ConfigHelper
def upload(url, file, key, username, password)
res = ''
url = URI.parse(url)
File.open(File.join(LIB_DIR,file)) do |conf|
req = Net::HTTP::Post::Multipart.new(url.path,
key => UploadIO.new(conf, "application/octet-stream", File.join(LIB_DIR,file)))
req.basic_auth username, password
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
res.body
end
end

class ConfigController < Shoes::Main
include ConfigHelper
url '/configuration', :index
Expand Down
100 changes: 0 additions & 100 deletions lib/controllers/race_controller.rb
Original file line number Diff line number Diff line change
@@ -1,103 +1,3 @@
module RaceHelper
def count_text(text)
banner text, :font => "140px", :align => 'center'
end

def left_names(race)
@left.clear {
stack do # start progress_bars
race.race_participations.each_with_index do |racer,i|
stack(:width => 1.0, :height => 81) {
background(eval(racer.color), :width => 1.0, :height => 70)
tagline(racer.racer.name, :margin => [10,10,0,0]).displace(0,0)
}
end
end.displace(0,162) # end progress_bars
}
end

def right_bars(race)
@right.clear {
stack do # start progress_bars
race.race_participations.each_with_index do |racer,i|
fill right_bar_color||gradient(rgb(230,230,230),rgb(167,167,167), :angle => -90)
stroke(black(0.0))
rect(4,81*i+162,@right.width,69)
end
end.displace(0,162) # end progress_bars
}
end

def progress_bars(race,speed=false)
stack do # start progress_bars
flow {
race.race_participations.each_with_index do |racer,i|
if speed
flow(:width => 0.5){
flow(:width => 0.5){
title(racer.speed(racer.finish_time||SENSOR.time), :align => 'right',:margin => [0]*4,
:stroke => eval(racer.color))
}
flow(:width => 0.5){
title("mph", :margin => [0]*4, :stroke => eval(racer.color))
}
}
else
flow(:width => 0.5){
title(" ", :margin => [20]*4)
}
end
end
}
race.race_participations.each_with_index do |racer,i|
fill gradient(rgb(230,230,230),rgb(167,167,167), :angle => -90)
stroke(black(0.0))
rect(-1,81*i+162,@center.width+1,69)
fill eval(racer.color)
rect(-1,81*i+162,@center.width*racer.percent_complete+1,69)
#oval(:radius => 34, :left => @center.width*racer.percent_complete+10, :top => 81*i+162)
end

end.displace(0,0) # end progress_bars
end

def stop_save_and_show_winner(race, id)
@race_animation.stop
SENSOR.stop
race.raced = true
race.save
race.race_participations.map(&:save)
visit "/races/#{id}/winner"
end

def swap_button(race,rerender_url)
button($i18n.swap) {
racers = race.racers.reverse
race.race_participations.each{|rp|rp.destroy}
racers.map {|r|
RaceParticipation.create(:racer => r, :race => race)
}
visit rerender_url
}
end

def render_racer_name_and_color(race_participation,id)
flow(:height => 0.3) {
background(eval(race_participation.color), :width=> 0.9)
}
flow(:height => 0.3) {
caption race_participation.racer.name
delete_button {
race_participation.destroy
visit "/races/#{id}/edit"
}
}
flow(:height => 0.3) {
background(eval(race_participation.color), :width=> 0.9)
}
end
end

class RaceController < Shoes::Main
include RaceHelper
url '/races/(\d+)/ready', :ready
Expand Down
59 changes: 0 additions & 59 deletions lib/controllers/racer_controller.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,3 @@
module RacerHelper
def create_categorizations_for_checkboxes(racer)
racer.categorizations.each{|cz|cz.destroy}
@checkboxes.each do |cb, category|
Categorization.create(:category => category, :racer => racer) if cb.checked?
end
end

def find_or_create_participation(racer, tournament)
TournamentParticipation.filter(:tournament_id => tournament.pk, :racer_id => racer.pk).first||TournamentParticipation.create(:racer => racer, :tournament => tournament)
end


def create_old_racer(old_racer, racer, tournament)
TournamentParticipation.create(:racer => old_racer, :tournament => tournament)
old_racer.save
create_categorizations_for_checkboxes(old_racer)
racer.destroy
end

def cancel_button(racer,after_url)
button $i18n.cancel do
racer.destroy if Racer[racer.pk].try(:name).blank?
# Delete all participations where the racer_id is not in racers.
TournamentParticipation.exclude(:id => TournamentParticipation.join(:racers, :id => :racer_id).select(:tournament_participations__id)).delete
visit after_url
end
end

def render_racer_form(racer, tournament_participation)
flow {
para $i18n.name
@e = edit_line(racer.name) do |edit|
racer.name = edit.text
end
}
separator_line
flow {
para $i18n.assign_to_categories
stack {
stack(:width => 0.5) {
@checkboxes = Category.all.map do |category|
flow { @c = c = check; para(category.name); click { c.toggle } }
@c.checked = racer.categories.include?(category)
[@c, category]
end
}
}
}
separator_line
flow {
c = check
c.click { |c| tournament_participation.eliminated = c.checked? }
c.checked = tournament_participation.eliminated
para $i18n.eliminated
}
end
end

class RacerController < Shoes::Main
include RacerHelper
url '/racers', :list
Expand Down
32 changes: 0 additions & 32 deletions lib/controllers/tournament_controller.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
module TournamentHelper
def stats_table(label,racers)
if racers.any?
stack do
container
flow(:height => 52) { title label+':', :font => "Bold" }
separator_line(80)
flow(:height => 20) do
stack(:width => 0.4) { para $i18n.name }
stack(:width => 0.2) { para $i18n.losses }
stack(:width => 0.2) { para $i18n.best }
stack(:width => 0.2) { para $i18n.place }
end
separator_line(80)
stack(:scroll => false) do
racers.each do |racer|
stack do
flow do
stack(:width => 0.4) { inscription racer.racer.name }
stack(:width => 0.2) { inscription racer.losses }
stack(:width => 0.18) { inscription((("%.2f" % racer.best_time) if racer.best_time)) }
flow(:width => 0.22) { inscription racer.rank }
end
end
end
end
end
end
end

end

class TournamentController < Shoes::Main
include TournamentHelper
include RacerHelper
Expand Down
13 changes: 13 additions & 0 deletions lib/helpers/audience_admin_seperation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module AudienceAdminSeperation
module InstanceMethods
def admin_window?; !owner; end
def audience_window?; !admin_window?; end
def audience_friendly_url?(url); $audience_visible_urls.any? { |re| url =~ re }; end
end
module ClassMethods
def audience_friendly_urls(*urls)
$audience_visible_urls ||= []
$audience_visible_urls += urls
end
end
end
15 changes: 15 additions & 0 deletions lib/helpers/config_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ConfigHelper
def upload(url, file, key, username, password)
res = ''
url = URI.parse(url)
File.open(File.join(LIB_DIR,file)) do |conf|
req = Net::HTTP::Post::Multipart.new(url.path,
key => UploadIO.new(conf, "application/octet-stream", File.join(LIB_DIR,file)))
req.basic_auth username, password
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
res.body
end
end
45 changes: 45 additions & 0 deletions lib/helpers/default_styles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module DefaultStyles
def custom_font
nil
end

def text_color
nil
end

def link_color
nil
end

def link_hover
nil
end

def link_hover_background
nil
end

def container_background
nil
end

def container_border
nil
end

def button_background
nil
end

def button_border
nil
end

def divider_color
nil
end

def right_bar_color
nil
end
end
79 changes: 79 additions & 0 deletions lib/helpers/main_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module MainHelper
def button(text, styles={}, &callback)
if admin_window?
stack(:height => 32, :width => styles[:width]||(40+(text.length * 8)), :margin => [5,10,5,0], :padding_top => 0) do
background(button_background||styles[:fill]||("#e5e6e6"..."#c1c2c4"), :curve => 1)
border(button_border||styles[:border]||"#ffcf01")
t = inscription(text, :align => styles[:align]||'center', :stroke => styles[:stroke]||black, :margin => styles[:margin]||[0]*4)
click &callback
hover {
self.cursor = :hand
}
leave {
self.cursor = :arrow
}
end
end
end

def left_button(text, styles={}, &callback)
button(text, styles.merge({:width => 1.0, :align => 'left', :margin => [10,0,0,0]}), &callback)
end

def light_button(text, styles={}, &callback)
button(text, styles.merge({}), &callback)
end

def image_button(path,styles={}, &callback)
if admin_window?
stack(:margin_top => 8, :width => 20) do
click &callback
b = background(link_hover_background||"#ffcf01")
image path
b.hide
hover {
b.show
self.cursor = :hand
}
leave {
b.hide
self.cursor = :arrow
}
end
end
end

def delete_button(styles={}, &callback)
image_button("media/cross.png", styles, &callback)
end
def edit_button(styles={}, &callback)
image_button("media/application_form_edit.png", styles, &callback)
end

def container
background(container_background||("#e5e6e6"..."#babcbe"), :curve => 1)
border(container_border||"#ffcf01")
end

def session
if(defined?(@@session) && @@session)
@@session
else
@@session = {:referrer => []}
end
end

def separator_line(n=45)
flow(:height => 18, :scroll => false) do
inscription '_'*n, :margin => [0]*4, :stroke => divider_color||"#ffcf01"
end
end

def small_logo
if(defined?(SKIN)&&File.exist?("media/skins/#{SKIN}/logo_text.png"))
image("media/skins/#{SKIN}/logo_text.png", :attach => Window, :top => 30, :left => WIDTH-110)
else
image("media/logo_text.png", :attach => Window, :top => 30, :left => WIDTH-110)
end
end
end
Loading

0 comments on commit d2734fe

Please sign in to comment.