diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 0e4b1b9..88a8fe7 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -1,6 +1,7 @@ require 'fastercsv' class QuestionsController < InheritedResources::Base + before_filter :authenticate respond_to :xml, :json respond_to :csv, :only => :export #leave the option for xml export here belongs_to :site, :optional => true @@ -49,7 +50,6 @@ class QuestionsController < InheritedResources::Base end def create - authenticate logger.info "all params are #{params.inspect}" logger.info "vi is #{params['question']['visitor_identifier']} and local are #{params['question']['local_identifier']}." if @question = current_user.create_question(params['question']['visitor_identifier'], :name => params['question']['name'], :local_identifier => params['question']['local_identifier'], :ideas => (params['question']['ideas'].lines.to_a.delete_if {|i| i.blank?})) @@ -66,7 +66,6 @@ class QuestionsController < InheritedResources::Base def set_autoactivate_ideas_from_abroad - authenticate expire_page :action => :index logger.info("INSIDE autoactivate ideas") @@ -88,8 +87,6 @@ class QuestionsController < InheritedResources::Base end def export - authenticate - type = params[:type] if type == 'votes' @@ -104,8 +101,6 @@ class QuestionsController < InheritedResources::Base end def num_votes_by_visitor_id - authenticate - @question = current_user.questions.find(params[:id]) hash = Vote.count(:conditions => "question_id = #{@question.id}", :group => "voter_id") visitor_id_hash = {} diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb index 40bb7f2..a548a00 100644 --- a/app/controllers/visitors_controller.rb +++ b/app/controllers/visitors_controller.rb @@ -1,85 +1,23 @@ -class VisitorsController < ApplicationController - # GET /visitors - # GET /visitors.xml - def index - @visitors = Visitor.all +class VisitorsController < InheritedResources::Base + respond_to :xml, :json + before_filter :authenticate + def votes_by_session_ids + session_ids = params[:session_ids] + + visitor_ids = Visitor.find(:all, :conditions => { :identifier => session_ids}) + votes_by_visitor_id = Vote.with_voter_ids(visitor_ids).count(:group => :voter_id) + + votes_by_session_id = {} + + visitor_ids.each do |e| + if votes_by_visitor_id.has_key?(e.id) + votes_by_session_id[e.identifier] = votes_by_visitor_id[e.id] + end + end + + respond_to do |format| + format.xml{ render :xml => votes_by_session_id.to_xml and return} + end + end - respond_to do |format| - format.html # index.html.erb - format.xml { render :xml => @visitors } - end - end - - # GET /visitors/1 - # GET /visitors/1.xml - def show - @visitor = Visitor.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @visitor } - end - end - - # GET /visitors/new - # GET /visitors/new.xml - def new - @visitor = Visitor.new - - respond_to do |format| - format.html # new.html.erb - format.xml { render :xml => @visitor } - end - end - - # GET /visitors/1/edit - def edit - @visitor = Visitor.find(params[:id]) - end - - # POST /visitors - # POST /visitors.xml - def create - @visitor = Visitor.new(params[:visitor]) - - respond_to do |format| - if @visitor.save - flash[:notice] = 'Visitor was successfully created.' - format.html { redirect_to(@visitor) } - format.xml { render :xml => @visitor, :status => :created, :location => @visitor } - else - format.html { render :action => "new" } - format.xml { render :xml => @visitor.errors, :status => :unprocessable_entity } - end - end - end - - # PUT /visitors/1 - # PUT /visitors/1.xml - def update - @visitor = Visitor.find(params[:id]) - - respond_to do |format| - if @visitor.update_attributes(params[:visitor]) - flash[:notice] = 'Visitor was successfully updated.' - format.html { redirect_to(@visitor) } - format.xml { head :ok } - else - format.html { render :action => "edit" } - format.xml { render :xml => @visitor.errors, :status => :unprocessable_entity } - end - end - end - - # DELETE /visitors/1 - # DELETE /visitors/1.xml - def destroy - @visitor = Visitor.find(params[:id]) - @visitor.destroy - - respond_to do |format| - format.html { redirect_to(visitors_url) } - format.xml { head :ok } - end - end end diff --git a/app/models/visitor.rb b/app/models/visitor.rb index 6b33dd9..9407012 100644 --- a/app/models/visitor.rb +++ b/app/models/visitor.rb @@ -7,8 +7,10 @@ class Visitor < ActiveRecord::Base has_many :clicks validates_presence_of :site, :on => :create, :message => "can't be blank" - validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id - +# validates_uniqueness_of :identifier, :on => :create, :message => "must be unique", :scope => :site_id + + named_scope :with_tracking, lambda { |*args| {:include => :votes, :conditions => { :identifier => args.first } }} + def owns?(question) questions.include? question end diff --git a/app/models/vote.rb b/app/models/vote.rb index 31f3ffd..5f62e24 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -8,4 +8,5 @@ class Vote < ActiveRecord::Base named_scope :recent, lambda { |*args| {:conditions => ["created_at > ?", (args.first || Date.today.beginning_of_day)]} } named_scope :with_question, lambda { |*args| {:conditions => {:question_id => args.first }} } + named_scope :with_voter_ids, lambda { |*args| {:conditions => {:voter_id=> args.first }} } end diff --git a/config/routes.rb b/config/routes.rb index e169381..f569b2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ ActionController::Routing::Routes.draw do |map| - map.resources :clicks + #map.resources :clicks + map.resources :visitors, :collection => {:votes_by_session_ids => :get} map.resources :questions, :member => { :num_votes_by_visitor_id => :get, :export => :post, :set_autoactivate_ideas_from_abroad => :put, :activate => :put, :suspend => :put}, :collection => {:recent_votes_by_question_id => :get} do |question| question.resources :items question.resources :prompts, :member => {:vote_left => :post, :vote_right => :post, :skip => :post, :vote => :post}, @@ -8,6 +9,7 @@ ActionController::Routing::Routes.draw do |map| end map.resources :algorithms map.connect "/questions/:question_id/prompts/:id/vote/:index", :controller => 'prompts', :action => 'vote' + -- libgit2 0.21.2