Class PromptsController
In: app/controllers/prompts_controller.rb
Parent: ApplicationController

Methods

create   list   show   valid_item_ids   view  

Public Instance methods

GET /prompts/create

Return

Array of length n. Prompts created for the question and voter. Returns a maximum of Constants::MAX_BATCH_PROMPTS at a time.

Options (params)

question_id<String>:Converted to integer. Must be greater than 0 and belong to the current user.
voter_id<String>:Converted to integer. Must be 0 or belong to the current user.
n<String>:Converted to integer. Set to 1 if less than 1. Set to MAX_BATCH_PROMPTS if greater

than MAX_BATCH_PROMPTS.

item_id<String>:Nil or comma seperated list of items to include in the prompt.

be returned.

prime<String>:If passed the probability of a prompt being generated will

be proportional to the number of times this prompt has been voted on in the past (note: you must submit prompt view requests on prompt viewing for this proportion to be accurate).

data<String>:If passed the item is returned with its data.
prompt_algorithm<String>:Converted to integer. If passed the specified

prompt algorithm will be used to generate the prompt. Can be an ID or name of prompt algorithm.

Raises

ArgumentError:If question less than 1 or doesnt belong to current user or if voter greater than 0

and doesn‘t belong to current user.

[Source]

    # File app/controllers/prompts_controller.rb, line 83
83:   def create
84:     @question_id = params[:question_id].to_i
85:     voter_id = params[:voter_id].to_i
86:     num = params[:n].to_i
87:     prompt_algorithm_id = parse_prompt_algo_id(params[:prompt_algorithm], params[:prime])
88:     @data = !params[:data].nil?
89:     if num < 1
90:       num = 1
91:     elsif num > Constants::MAX_BATCH_PROMPTS
92:       num = Constants::MAX_BATCH_PROMPTS
93:     end
94:     raise ArgumentError unless current_user.questions.exists?(@question_id) && (voter_id < 1 || current_user.voters.exists?(voter_id))
95:     @prompt_item_ids = Prompt.fetch(@question_id, voter_id, num, prompt_algorithm_id)
96:     @algorithm_id = prompt_algorithm_id || 2
97:     raise ArgumentError if @prompt_item_ids.keys.empty?
98:   end

GET /prompts/list

Return

Array of length n. Prompts matching parameters

Options (params)

question_id<String>:Converted to integer. Must be greater than 0 and

belong to the current user. Must belong to user.

item_ids<String>:Comma seperated list of items to include. May only

include commas and digits. Must belong to user. Optional value.

data<String>:Flag for whether to include item data. Data included

if value is not nil.

Raises

PermissionError:If question or any item doesn‘t belong to current user.

[Source]

    # File app/controllers/prompts_controller.rb, line 42
42:   def list
43:     question_id = params[:question_id].to_i
44:     item_ids = params[:item_id]
45:     @data = !params[:data].nil?
46:     item_ids = valid_item_ids(item_ids)
47:     options =  { :include => :items, :conditions => {} }
48:     if question_id > 0
49:       options[:conditions].merge!('prompts.question_id' => question_id)
50:       raise PermissionError unless current_user.question_ids.include?(question_id)
51:       unless item_ids.empty?
52:         raise PermissionError unless (item_ids - current_user.item_ids).empty?
53:         options[:conditions].merge!({ 'items.id' => item_ids })
54:       end
55:       @prompts = Prompt.all(options)
56:     else
57:       @prompts = []
58:     end
59:   end

GET /prompts/1

Return

Prompt.

Options (params)

id<String>:Converted to integer. ID of prompt.

Raises

PermissionError:If prompt does not belong to user.

[Source]

    # File app/controllers/prompts_controller.rb, line 11
11:   def show
12:     @prompt = Prompt.find(params[:id])
13:     raise PermissionError unless current_user.question_ids.include?(@prompt.question_id)
14:   end

GET /prompts/view/1

Return

Nothing.

Options (params)

id<String>:Converted to integer. ID of prompt to register view for.

Raises

PermissionError:If prompt does not belong to user.

[Source]

    # File app/controllers/prompts_controller.rb, line 23
23:   def view
24:     prompt = Prompt.find(params[:id])
25:     raise PermissionError unless current_user.question_ids.include?(prompt.question_id)
26:     Stat.view(prompt.question_id, prompt.items)
27:     head :ok
28:   end

Private Instance methods

If non "," or digit is passed as an "item_id" param all item params are ignored

[Source]

     # File app/controllers/prompts_controller.rb, line 110
110:     def valid_item_ids(item_ids) #:doc:
111:       (item_ids && !item_ids.empty? && item_ids.gsub(/,|\d/, '').empty?) ? item_ids.split(',').map(&:to_i) : []
112:     end

[Validate]