diff --git a/app/controllers/choices_controller.rb b/app/controllers/choices_controller.rb index 50ba099..5c62e02 100644 --- a/app/controllers/choices_controller.rb +++ b/app/controllers/choices_controller.rb @@ -12,8 +12,8 @@ class ChoicesController < InheritedResources::Base find_options = {:conditions => {:question_id => @question.id}, :limit => params[:limit].to_i, - :order => 'score DESC', - :include => :item} + :order => 'score DESC' + } find_options[:conditions].merge!(:active => true) unless params[:include_inactive] find_options.merge!(:offset => params[:offset]) if params[:offset] @@ -23,9 +23,9 @@ class ChoicesController < InheritedResources::Base else @question = Question.find(params[:question_id], :include => :choices) #eagerloads ALL choices unless params[:include_inactive] - @choices = @question.choices(true).active.find(:all, :include => :item) + @choices = @question.choices(true).active.find(:all) else - @choices = @question.choices.find(:all, :include =>:item) + @choices = @question.choices.find(:all) end end index! do |format| diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb deleted file mode 100644 index b2afd73..0000000 --- a/app/controllers/items_controller.rb +++ /dev/null @@ -1,86 +0,0 @@ -class ItemsController < ApplicationController - before_filter :authenticate - # GET /items - # GET /items.xml - def index - @items = Item.all - - respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @items } - end - end - - # GET /items/1 - # GET /items/1.xml - def show - @item = Item.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @item } - end - end - - # GET /items/new - # GET /items/new.xml - def new - @item = Item.new - - respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @item } - end - end - - # GET /items/1/edit - def edit - @item = Item.find(params[:id]) - end - - # POST /items - # POST /items.xml - def create - @item = Item.new(params[:item]) - - respond_to do |format| - if @item.save - flash[:notice] = 'Item was successfully created.' - format.html { redirect_to(@item) } - format.xml { render :xml => @item, :status => :created, :location => @item } - else - format.html { render :action => "new" } - format.xml { render :xml => @item.errors, :status => :unprocessable_entity } - end - end - end - - # PUT /items/1 - # PUT /items/1.xml - def update - @item = Item.find(params[:id]) - - respond_to do |format| - if @item.update_attributes(params[:item]) - flash[:notice] = 'Item was successfully updated.' - format.html { redirect_to(@item) } - format.xml { head :ok } - else - format.html { render :action => "edit" } - format.xml { render :xml => @item.errors, :status => :unprocessable_entity } - end - end - end - - # DELETE /items/1 - # DELETE /items/1.xml - def destroy - @item = Item.find(params[:id]) - @item.destroy - - respond_to do |format| - format.html { redirect_to(items_url) } - format.xml { head :ok } - end - end -end diff --git a/app/controllers/prompts_controller.rb b/app/controllers/prompts_controller.rb index 5ba4ca6..c80eaad 100644 --- a/app/controllers/prompts_controller.rb +++ b/app/controllers/prompts_controller.rb @@ -83,7 +83,7 @@ class PromptsController < InheritedResources::Base appearance_id = Proc.new { |options| options[:builder].tag!('appearance_id', @a.lookup) } visitor_votes = Proc.new { |options| options[:builder].tag!('visitor_votes', visitor.votes.count(:conditions => {:question_id => @question.id})) } - visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.items.count) } + visitor_ideas = Proc.new { |options| options[:builder].tag!('visitor_ideas', visitor.choices.count) } format.xml { render :xml => @next_prompt.to_xml(:procs => [appearance_id, visitor_votes, visitor_ideas],:methods => [:left_choice_text, :right_choice_text]), :status => :ok } @@ -98,7 +98,7 @@ class PromptsController < InheritedResources::Base def show @question = current_user.questions.find(params[:question_id]) - @prompt = @question.prompts.find(params[:id], :include => [{ :left_choice => :item }, { :right_choice => :item }]) + @prompt = @question.prompts.find(params[:id], :include => [:left_choice ,:right_choice ]) show! do |format| format.xml { render :xml => @prompt.to_xml(:methods => [:left_choice_text, :right_choice_text])} format.json { render :json => @prompt.to_json(:methods => [:left_choice_text, :right_choice_text])} diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 0c68c69..3e1eadd 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -27,12 +27,12 @@ class QuestionsController < InheritedResources::Base end def object_info_totals_by_question_id - total_ideas_by_q_id = Choice.count(:include => ['item', 'question'], - :conditions => "items.creator_id <> questions.creator_id", + total_ideas_by_q_id = Choice.count(:include => :question, + :conditions => "choices.creator_id <> questions.creator_id", :group => "choices.question_id") - active_ideas_by_q_id = Choice.count(:include => ['item', 'question'], - :conditions => "choices.active = 1 AND items.creator_id <> questions.creator_id", + active_ideas_by_q_id = Choice.count(:include => :question, + :conditions => "choices.active = 1 AND choices.creator_id <> questions.creator_id", :group => "choices.question_id") combined_hash = {} @@ -174,8 +174,7 @@ class QuestionsController < InheritedResources::Base elsif object_type == "uploaded_ideas" uploaded_ideas_by_visitor_id = @question.choices.find(:all, :select => 'creator_id, count(*) as ideas_count', - :joins => [:item], - :conditions => "items.creator_id != #{@question.creator_id}", + :conditions => "choices.creator_id != #{@question.creator_id}", :group => 'creator_id') count = 0 @@ -255,8 +254,7 @@ class QuestionsController < InheritedResources::Base elsif object_type == 'skips' hash = Skip.count(:conditions => {:question_id => @question.id}, :group => "date(created_at)") elsif object_type == 'user_submitted_ideas' - hash = Choice.count(:include => 'item', - :conditions => "choices.question_id = #{@question.id} AND items.creator_id <> #{@question.creator_id}", + hash = Choice.count(:conditions => "choices.question_id = #{@question.id} AND choices.creator_id <> #{@question.creator_id}", :group => "date(choices.created_at)") # we want graphs to go from date of first vote -> date of last vote, so adding those two boundries here. mindate = Vote.minimum('date(created_at)', :conditions => {:question_id => @question.id}) @@ -299,8 +297,8 @@ class QuestionsController < InheritedResources::Base if object_type == 'votes' hash = Vote.count(:group => "date(created_at)") elsif object_type == 'user_submitted_ideas' - hash = Choice.count(:include => ['item', 'question'], - :conditions => "items.creator_id <> questions.creator_id", + hash = Choice.count(:include => :question, + :conditions => "choices.creator_id <> questions.creator_id", :group => "date(choices.created_at)") elsif object_type == 'user_sessions' result = Vote.find(:all, :select => 'date(created_at) as date, voter_id, count(*) as vote_count', diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb index 882f0cc..97a2271 100644 --- a/app/controllers/visitors_controller.rb +++ b/app/controllers/visitors_controller.rb @@ -7,7 +7,7 @@ class VisitorsController < InheritedResources::Base visitor_ids = Visitor.find(:all, :conditions => { :identifier => session_ids}) votes_by_visitor_id = Vote.with_voter_ids(visitor_ids).count(:group => :voter_id) - ideas_by_visitor_id = Item.with_creator_ids(visitor_ids).count(:group => :creator_id) + ideas_by_visitor_id = Choice.count(:group => :creator_id) objects_by_session_id = {} diff --git a/app/helpers/items_helper.rb b/app/helpers/items_helper.rb deleted file mode 100644 index cff0c9f..0000000 --- a/app/helpers/items_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module ItemsHelper -end diff --git a/app/models/choice.rb b/app/models/choice.rb index 7ab2fc8..f8ca4ba 100644 --- a/app/models/choice.rb +++ b/app/models/choice.rb @@ -1,7 +1,6 @@ class Choice < ActiveRecord::Base belongs_to :question, :counter_cache => true - belongs_to :item belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" validates_presence_of :creator, :on => :create, :message => "can't be blank" @@ -48,11 +47,6 @@ class Choice < ActiveRecord::Base end def before_create - #puts "just got inside choice#before_create. is set to active? #{self.active?}" - unless item - @item = Item.create!(:creator => creator, :data => data) - self.item = @item - end unless self.score self.score = 50.0 end @@ -75,7 +69,7 @@ class Choice < ActiveRecord::Base end def user_created - self.item.creator_id != self.question.creator_id + self.creator_id != self.question.creator_id end def compute_bt_score(btprobs = nil) diff --git a/app/models/item.rb b/app/models/item.rb deleted file mode 100644 index 5ce0be6..0000000 --- a/app/models/item.rb +++ /dev/null @@ -1,24 +0,0 @@ -class Item < ActiveRecord::Base - belongs_to :question, :counter_cache => true - belongs_to :site, :class_name => "User", :foreign_key => "site_id" - belongs_to :creator, :class_name => "Visitor", :foreign_key => "creator_id" - - named_scope :active, :conditions => { :active => true } - named_scope :with_creator_ids, lambda { |*args| {:conditions => {:creator_id=> args.first }} } - - # has_many :items_questions, :dependent => :destroy - # has_many :questions, :through => :items_questions - # has_and_belongs_to_many :prompts - # - # has_and_belongs_to_many :votes - # has_and_belongs_to_many :prompt_requests - - validates_presence_of :creator_id - validates_presence_of :data, :on => :create, :message => "can't be blank" - - def self.mass_insert!(creator_id, data_array) - #alpha - inserts = data_array.collect{|i| "(#{connection.quote i}, #{connection.quote creator_id})"}.join(", ") - connection.insert("INSERT INTO items(data, creator_id) VALUES (#{inserts})") - end -end diff --git a/app/models/prompt.rb b/app/models/prompt.rb index 506a1b0..e5bc269 100644 --- a/app/models/prompt.rb +++ b/app/models/prompt.rb @@ -10,6 +10,9 @@ class Prompt < ActiveRecord::Base belongs_to :left_choice, :class_name => "Choice", :foreign_key => "left_choice_id", :counter_cache => true belongs_to :right_choice, :class_name => "Choice", :foreign_key => "right_choice_id", :counter_cache => true + validates_presence_of :left_choice, :on => :create, :message => "can't be blank" + validates_presence_of :right_choice, :on => :create, :message => "can't be blank" + named_scope :with_left_choice, lambda { |*args| {:conditions => ["left_choice_id = ?", (args.first.id)]} } named_scope :with_right_choice, lambda { |*args| {:conditions => ["right_choice_id = ?", (args.first.id)]} } named_scope :with_choice, lambda { |*args| {:conditions => ["(right_choice_id = ?) OR (left_choice_id = ?)", (args.first.id)]} } @@ -25,8 +28,6 @@ class Prompt < ActiveRecord::Base select {|z| z.voted_on_by_user?(u)} end - validates_presence_of :left_choice, :on => :create, :message => "can't be blank" - validates_presence_of :right_choice, :on => :create, :message => "can't be blank" def choices [left_choice, right_choice] @@ -37,7 +38,7 @@ class Prompt < ActiveRecord::Base end def left_choice_text(prompt = nil) - left_choice.item.data + left_choice.data end def active? @@ -46,7 +47,7 @@ class Prompt < ActiveRecord::Base def right_choice_text(prompt = nil) - right_choice.item.data + right_choice.data end end diff --git a/app/models/question.rb b/app/models/question.rb index c2cd40b..0f85e8d 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -26,8 +26,7 @@ class Question < ActiveRecord::Base def create_choices_from_ideas if ideas && ideas.any? ideas.each do |idea| - item = Item.create!(:data => idea.squish.strip, :creator => self.creator) - choices.create!(:item => item, :creator => self.creator, :active => true, :data => idea.squish.strip) + choices.create!(:creator => self.creator, :active => true, :data => idea.squish.strip) end end end @@ -70,7 +69,7 @@ class Question < ActiveRecord::Base raise NotImplementedError.new("Sorry, we currently only support pairwise prompts. Rank of the prompt must be 2.") unless rank == 2 begin choice_id_array = distinct_array_of_choice_ids(rank) - @p = prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [{ :left_choice => :item }, { :right_choice => :item }]) + @p = prompts.find_or_create_by_left_choice_id_and_right_choice_id(choice_id_array[0], choice_id_array[1], :include => [:left_choice ,:right_choice ]) logger.info "#{@p.inspect} is active? #{@p.active?}" end until @p.active? return @p @@ -173,7 +172,7 @@ class Question < ActiveRecord::Base if params[:with_visitor_stats] visitor = current_user.visitors.find_or_create_by_identifier(visitor_identifier) result.merge!(:visitor_votes => visitor.votes.count(:conditions => {:question_id => self.id})) - result.merge!(:visitor_ideas => visitor.items.count) + result.merge!(:visitor_ideas => visitor.choices.count) end return result @@ -303,14 +302,6 @@ class Question < ActiveRecord::Base picked_prompt.id end - def left_choice_text(prompt = nil) - picked_prompt.left_choice.item.data - end - - def right_choice_text(prompt = nil) - picked_prompt.right_choice.item.data - end - def self.voted_on_by(u) select {|z| z.voted_on_by_user?(u)} end @@ -516,7 +507,7 @@ class Question < ActiveRecord::Base num_skips = self.skips.count(:conditions => {:prompt_id => left_prompts_ids + right_prompts_ids}) csv << [c.question_id, c.id, "'#{c.data.strip}'", c.wins, c.losses, num_skips, c.score, - user_submitted , c.item.creator_id, c.created_at, c.updated_at, c.active, + user_submitted , c.creator_id, c.created_at, c.updated_at, c.active, left_appearances, right_appearances] end when 'non_votes' diff --git a/app/models/user.rb b/app/models/user.rb index 4a48606..68628c5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,6 @@ class User < ActiveRecord::Base has_many :visitors, :class_name => "Visitor", :foreign_key => "site_id" has_many :questions, :class_name => "Question", :foreign_key => "site_id" has_many :clicks, :class_name => "Click", :foreign_key => "site_id" - has_many :items, :class_name => "Item", :foreign_key => "site_id" def default_visitor visitors.find(:first, :conditions => {:identifier => 'owner'}) diff --git a/app/models/visitor.rb b/app/models/visitor.rb index c6e1983..b7e491e 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -3,7 +3,7 @@ class Visitor < ActiveRecord::Base has_many :questions, :class_name => "Question", :foreign_key => "creator_id" has_many :votes, :class_name => "Vote", :foreign_key => "voter_id" has_many :skips, :class_name => "Skip", :foreign_key => "skipper_id" - has_many :items, :class_name => "Item", :foreign_key => "creator_id" + has_many :choices, :class_name => "Choice", :foreign_key => "creator_id" has_many :clicks has_many :appearances, :foreign_key => "voter_id" diff --git a/app/views/items/edit.html.erb b/app/views/items/edit.html.erb deleted file mode 100644 index 0f96ad0..0000000 --- a/app/views/items/edit.html.erb +++ /dev/null @@ -1,12 +0,0 @@ -

Editing item

- -<% form_for(@item) do |f| %> - <%= f.error_messages %> - -

- <%= f.submit 'Update' %> -

-<% end %> - -<%= link_to 'Show', @item %> | -<%= link_to 'Back', items_path %> \ No newline at end of file diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb deleted file mode 100644 index 61cf6ab..0000000 --- a/app/views/items/index.html.erb +++ /dev/null @@ -1,18 +0,0 @@ -

Listing items

- - - - - -<% @items.each do |item| %> - - - - - -<% end %> -
<%= link_to 'Show', item %><%= link_to 'Edit', edit_item_path(item) %><%= link_to 'Destroy', item, :confirm => 'Are you sure?', :method => :delete %>
- -
- -<%= link_to 'New item', new_item_path %> \ No newline at end of file diff --git a/app/views/items/new.html.erb b/app/views/items/new.html.erb deleted file mode 100644 index 369564c..0000000 --- a/app/views/items/new.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -

New item

- -<% form_for(@item) do |f| %> - <%= f.error_messages %> - -

- <%= f.submit 'Create' %> -

-<% end %> - -<%= link_to 'Back', items_path %> \ No newline at end of file diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb deleted file mode 100644 index 7305cd8..0000000 --- a/app/views/items/show.html.erb +++ /dev/null @@ -1,3 +0,0 @@ - -<%= link_to 'Edit', edit_item_path(@item) %> | -<%= link_to 'Back', items_path %> \ No newline at end of file diff --git a/app/views/layouts/items.html.erb b/app/views/layouts/items.html.erb deleted file mode 100644 index 0624b95..0000000 --- a/app/views/layouts/items.html.erb +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - Items: <%= controller.action_name %> - <%= stylesheet_link_tag 'scaffold' %> - - - -

<%= flash[:notice] %>

- -<%= yield %> - - - diff --git a/spec/models/choice_spec.rb b/spec/models/choice_spec.rb index 0b2b6ca..c892bbe 100644 --- a/spec/models/choice_spec.rb +++ b/spec/models/choice_spec.rb @@ -3,7 +3,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe Choice do it {should belong_to :question} - it {should belong_to :item} it {should belong_to :creator} it {should have_many :flags} it {should have_many :votes} diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb deleted file mode 100644 index e06eb3b..0000000 --- a/spec/models/item_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') - -describe Item do - it {should belong_to :creator} - it {should belong_to :site} - it {should validate_presence_of :data} - - before(:each) do - @aoi_clone = Factory.create(:user, :email => "pius@alum.mit.edu", :password => "password", :password_confirmation => "password", :id => 8) - @johndoe = Factory.create(:visitor, :identifier => 'johndoe', :site => @aoi_clone) - @valid_attributes = { - :site => @aoi_clone, - :creator => @johndoe, - :data => 'a widget' - } - end - - it "should create a new instance given valid attributes" do - Item.create!(@valid_attributes) - end -end diff --git a/spec/models/prompt_spec.rb b/spec/models/prompt_spec.rb index a8dd846..48e8223 100644 --- a/spec/models/prompt_spec.rb +++ b/spec/models/prompt_spec.rb @@ -4,5 +4,16 @@ describe Prompt do it {should belong_to :question} it {should belong_to :left_choice} it {should belong_to :right_choice} + before(:each) do + @question = Factory.create(:aoi_question) + @prompt = @question.prompts.first + end + + it "should display left choice text" do + @prompt.left_choice_text.should == @prompt.left_choice.data + end + it "should display right choice text" do + @prompt.right_choice_text.should == @prompt.right_choice.data + end end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index a6d61f5..044468d 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -112,6 +112,12 @@ describe Question do @question_optional_information[:picked_prompt_id].should == saved_prompt_id end + it "should auto create ideas when 'ideas' attribute is set" do + @question = Factory.build(:question) + @question.ideas = %w(one two three) + @question.save + @question.choices.count.should == 3 + end context "catchup algorithm" do before(:all) do @catchup_q = Factory.create(:aoi_question) @@ -277,7 +283,6 @@ describe Question do rows.first.should include("Record ID") rows.first.should include("Record Type") rows.first.should_not include("Idea ID") - puts filename File.delete(filename).should_not be_nil @@ -294,7 +299,6 @@ describe Question do rows = FasterCSV.read(filename) rows.first.should include("Idea ID") rows.first.should_not include("Skip ID") - puts filename File.delete(filename).should_not be_nil end diff --git a/spec/models/visitor_spec.rb b/spec/models/visitor_spec.rb index 39a1a7e..871f75b 100644 --- a/spec/models/visitor_spec.rb +++ b/spec/models/visitor_spec.rb @@ -8,6 +8,7 @@ describe Visitor do it {should have_many :skips} it {should have_many :clicks} it {should have_many :appearances} + it {should have_many :choices} before(:each) do @question = Factory.create(:aoi_question) -- libgit2 0.21.2