Skip to content

Commit

Permalink
improved contest_options
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto committed Dec 10, 2008
1 parent 4543f8f commit 3166fa9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
15 changes: 12 additions & 3 deletions generators/contestable/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class BecomeContestableMigration < ActiveRecord::Migration
def self.up
create_table :contest_options, :force => true do |t|
t.references :contest, :polymorphic => true, :null => false
t.boolean :nominating, :default => false
t.integer :max_winners, :default => 0
t.boolean :by_nominations, :default => false
t.integer :max_allowed_winners, :default => 0
end
add_index :contest_options, ["contest_id", "contest_type"], :name => "fk_options_contests"

Expand All @@ -26,11 +26,20 @@ def self.up
add_index :votes, ["voter_id", "voter_type"], :name => "fk_voters"
add_index :votes, ["voteable_id", "voteable_type"], :name => "fk_voteables"
add_index :votes, ["contest_id", "contest_type"], :name => "fk_votes_contests"

create_table :contest_winners, :force => true do |t|
t.references :contest, :polymorphic => true, :null => false
t.references :contestable, :polymorphic => true, :null => false
t.text :notes
end

add_index :contest_winners, ["contest_id", "contest_type"], :name => "fk_winners_contests"
add_index :contest_winners, ["contestable_id", "contestable_type"], :name => "fk_winners_contestables"
end

def self.down
drop_table :nominations
drop_table :votes
drop_table :contest_options
end
end
end
1 change: 1 addition & 0 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'models/nomination'
require 'models/vote'
require 'models/contest_winner'
require 'models/contest_option'
ActiveRecord::Base.send(:include,BecomeContestable)
ActiveRecord::Base.send(:include,BecomeContest)
ActiveRecord::Base.send(:include,BecomeVoter)
17 changes: 15 additions & 2 deletions lib/become_contest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ def self.included(base)
end

module ActMethods
def become_contest
def become_contest(options = {})
has_many :votes, :as => :voteable
has_many :nominations, :as => :nominatable
has_one :contest_option, :as => :contest
has_many :winners, :as => :contest, :class_name => "ContestWinner"

unless included_modules.include? InstanceMethods
before_save :save_contest_options

unless included_modules.include? InstanceMethods
class_inheritable_accessor :become_contest_options
extend ClassMethods
include InstanceMethods
end

self.become_contest_options = options
end

module InstanceMethods
Expand All @@ -26,6 +31,14 @@ def top(length = 10)

module ClassMethods

def save_contest_options
self.build_contest_option(self.class.become_contest_options) if self.contest_options = nil
end

def contest_option_attributes=(contest_option_attributes)
self.contest_option = ContestOption.new(contest_option_attributes.reverse_merge(self.class.become_contest_options))
end

end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/models/contest_option.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ContestOption < ActiveRecord::Base

belongs_to :contest, :polymorphic => true

end
27 changes: 16 additions & 11 deletions test/config/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,40 @@
ActiveRecord::Schema.define do
create_table :contest_options, :force => true do |t|
t.references :contest, :polymorphic => true, :null => false
t.boolean :nominating, :default => false
t.boolean :by_nominations, :default => false
t.integer :max_allowed_winners, :default => 0
end

create_table :contest_winners, :force => true do |t|
t.references :contest, :polymorphic => true, :null => false
t.references :contestable, :polymorphic => true, :null => false
t.text :notes
end

add_index :contest_options, ["contest_id", "contest_type"], :name => "fk_options_contests"

create_table :nominations, :force => true do |t|
t.references :nominatable, :polymorphic => true, :null => false
t.references :contest, :polymorphic => true, :null => false
end

add_index :nominations, ["nominatable_id", "nominatable_type"], :name => "fk_nominatables"
add_index :nominations, ["contest_id", "contest_type"], :name => "fk_nominations_contests"

create_table :votes, :force => true do |t|
t.boolean :vote, :default => true
t.references :voteable, :polymorphic => true, :null => false
t.references :voter, :polymorphic => true
t.references :contest, :polymorphic => true, :null => false
t.datetime :created_at
end

add_index :votes, ["voter_id", "voter_type"], :name => "fk_voters"
add_index :votes, ["voteable_id", "voteable_type"], :name => "fk_voteables"
add_index :votes, ["contest_id", "contest_type"], :name => "fk_votes_contests"

create_table :contest_winners, :force => true do |t|
t.references :contest, :polymorphic => true, :null => false
t.references :contestable, :polymorphic => true, :null => false
t.text :notes
end

add_index :contest_winners, ["contest_id", "contest_type"], :name => "fk_winners_contests"
add_index :contest_winners, ["contestable_id", "contestable_type"], :name => "fk_winners_contestables"

create_table :users, :force => true do |t|
t.string :login
end
Expand Down
9 changes: 6 additions & 3 deletions test/fixtures/contest_options.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
one:
contest_id: 1
contest_type: Contest
nominating: false
by_nomination: false
max_allowed_winners: 2

two:
contest_id: 2
contest_type: Contest
nominating: true
by_nomination: true
max_allowed_winners: 1

three:
contest_id: 3
contest_type: Contest
nominating: true
by_nomination: true
max_allowed_winners: 0

0 comments on commit 3166fa9

Please sign in to comment.