Class: Form
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Form
- Defined in:
- app/models/form.rb
Constant Summary collapse
- @@captcha_questions =
[]
Class Method Summary collapse
- .captcha_question(cached_quantity = 100) ⇒ Object
- .debar_counts(data) ⇒ Object
- .encrypt_answer(answer) ⇒ Object
- .encrypt_answers(answers) ⇒ Object
- .get_textcaptcha_qa ⇒ Object
- .load_captcha_questions ⇒ Object
- .md5_answer(answer) ⇒ Object
- .validate_captcha_answer(answer, answers) ⇒ Object
Instance Method Summary collapse
- #field_by_name(name) ⇒ Object
- #geo_codeable_fields ⇒ Object
- #include_stylesheets ⇒ Object
- #record(id) ⇒ Object
-
#records(options = {}) ⇒ Object
field_conditions => [ :comparator=>“=”, :value=>“Test”, :connective=>“and”, { … } ].
- #text_captcha_entry ⇒ Object
- #use_text_captcha?(request, user, even_if_logged_in = false) ⇒ Boolean
Class Method Details
.captcha_question(cached_quantity = 100) ⇒ Object
150 151 152 153 154 155 156 |
# File 'app/models/form.rb', line 150 def self.captcha_question(cached_quantity = 100) if @@captcha_questions.size < cached_quantity logger.debug "Currently have #{@@captcha_questions.size} captcha questions, loading another one" return Form.load_captcha_questions end return @@captcha_questions[rand(l).to_i] end |
.debar_counts(data) ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/form.rb', line 131 def Form.(data) values = {} data.each do |name, count| name.split('|').each do |nm| values[nm] = (values[nm] || 0) + count end end return values end |
.encrypt_answer(answer) ⇒ Object
179 180 181 |
# File 'app/models/form.rb', line 179 def self.encrypt_answer(answer) BCrypt::Engine.hash_secret(answer, "$2a$10$YCymaxgU1LZq5Tij07wvOu", 3) end |
.encrypt_answers(answers) ⇒ Object
175 176 177 |
# File 'app/models/form.rb', line 175 def self.encrypt_answers(answers) answers.map { |answer| encrypt_answer(answer) }.join('-') end |
.get_textcaptcha_qa ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/form.rb', line 158 def self.get_textcaptcha_qa xml = Net::HTTP.get(URI::Parser.new.parse("http://textcaptcha.com/api/7zsxcgrbaacc4cs4swsswk4sk863uwld")) if xml.empty? raise Textcaptcha::BadResponse else parsed_xml = ActiveSupport::XmlMini.parse(xml)['captcha'] spam_question = parsed_xml['question']['__content__'] if parsed_xml['answer'].is_a?(Array) spam_answers = encrypt_answers(parsed_xml['answer'].collect { |a| a['__content__'] }) else spam_answers = encrypt_answers([parsed_xml['answer']['__content__']]) end return spam_question, spam_answers end end |
.load_captcha_questions ⇒ Object
143 144 145 146 147 148 |
# File 'app/models/form.rb', line 143 def self.load_captcha_questions question, answers = Form.get_textcaptcha_qa a = [question, answers] @@captcha_questions << a return a end |
.md5_answer(answer) ⇒ Object
183 184 185 |
# File 'app/models/form.rb', line 183 def self.md5_answer(answer) Digest::MD5.hexdigest(answer.to_s.strip.mb_chars.downcase) end |
.validate_captcha_answer(answer, answers) ⇒ Object
187 188 189 |
# File 'app/models/form.rb', line 187 def self.validate_captcha_answer(answer, answers) (answer && answers) ? answers.split('-').include?(encrypt_answer(md5_answer(answer))) : false end |
Instance Method Details
#field_by_name(name) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'app/models/form.rb', line 24 def field_by_name(name) self.form_fields.each do |ff| if ff.code_name == name.to_s return ff end end return nil end |
#geo_codeable_fields ⇒ Object
15 16 17 |
# File 'app/models/form.rb', line 15 def geo_codeable_fields self.form_fields.joins(:form_field_type).where("form_field_types.field_type in ('line','paragraph','select','multiselect')").all end |
#include_stylesheets ⇒ Object
19 20 21 22 |
# File 'app/models/form.rb', line 19 def include_stylesheets return ["application"] if self.stylesheets.is_blank? return self.stylesheets.split(',').uniq end |
#record(id) ⇒ Object
33 34 35 |
# File 'app/models/form.rb', line 33 def record(id) self.form_submissions.sys(self.system_id).where(:id=>id).includes({:form=>{:form_field_groups=>:form_fields}}).first end |
#records(options = {}) ⇒ Object
field_conditions => [ :comparator=>“=”, :value=>“Test”, :connective=>“and”, { … } ]
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/models/form.rb', line 39 def records( = {}) field_conditions = [:field_conditions] r = self.form_submissions.includes(:form_submission_fields) r = r.where(:visible => 1) if [:only_visible] r = r.where([:where]) if [:where] if [:enforce_permissions] if self.public_visible==0 if self.user_visible==0 if self.owner_visible==0 return [] else r = r.where("form_submissions.user_id = #{[:user_id]}") end else if [:user_id]==nil return [] end end end end if field_conditions conditions = {} if field_conditions.is_a?(Array) field_conditions.each do |field_condition| field_condition[:connective] ||= "and" that_field = self.field_by_name(field_condition[:name]) conditions[that_field.id] = [field_condition[:comparator], field_condition[:value], field_condition[:connective]] if that_field end elsif field_conditions.is_a?(Hash) field_conditions.each do |field_name, field_value| that_field = self.field_by_name(field_name) conditions[that_field.id] = ['=', field_value, "and"] end else raise "record conditions can be a hash of equalities, or an array of {:name, :value, :comparator, [:connective]} hashes" end alias_index = 0 conditions.each do |field_id, field_comparison| alias_index += 1 r = r.joins("inner join form_submission_fields fsf#{alias_index} on fsf#{alias_index}.form_submission_id = form_submissions.id and fsf#{alias_index}.form_field_id = #{field_id}") end alias_index = 0 where_clause = " (1=1) " bind_values = {} conditions.each do |field_id, field_comparison| alias_index += 1 where_clause += " #{field_comparison[2]} fsf#{alias_index}.value #{field_comparison[0]} :field_#{field_id} " bind_values["field_#{field_id}".to_sym] = field_comparison[1] end r = r.where(where_clause, bind_values) end if [:order_by] [:order_by] = [ [:order_by] ] if [:order_by].is_a?(String) sorting_index = 0 [:order_by].each do |order_by| that_field = self.field_by_name(order_by) sorting_index += 1 r = r.joins("inner join form_submission_fields fsf_sort#{sorting_index} on fsf_sort#{sorting_index}.form_submission_id = form_submissions.id and fsf_sort#{sorting_index}.form_field_id = #{that_field.id}") r = r.order("fsf_sort#{sorting_index}.value") end end if [:group_values] that_field = self.field_by_name([:group_values]) r = r.joins("inner join form_submission_fields fsf_grouping on fsf_grouping.form_submission_id = form_submissions.id and fsf_grouping.form_field_id = #{that_field.id}") r = r.count(:group=>"fsf_grouping.value", :order=>"count_all desc") end if [:random] r = r.limit(1) r = r.order(:id) max_id = self.form_submissions.maximum("form_submissions.id") min_id = self.form_submissions.minimum("form_submissions.id") results = [] for int in 1..[:random] id = rand(max_id-min_id) + min_id - 1 s = r.clone s = s.where("form_submissions.id >= #{id}") results += s.all end return results else return r end end |
#text_captcha_entry ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'app/models/form.rb', line 199 def text_captcha_entry question, answers = Form.captcha_question(500) "<div class='q_a'> <input type='hidden' name='q_q' value='#{answers}'/> <label>#{question}</label> <input type='text' name='q_a' value='' class='captcha'> </div>".html_safe end |
#use_text_captcha?(request, user, even_if_logged_in = false) ⇒ Boolean
191 192 193 194 195 196 197 |
# File 'app/models/form.rb', line 191 def use_text_captcha?(request, user, even_if_logged_in = false) country = IpCountry.country_from_ip(request.remote_ip) risk = country ? country.risk : 10 use_captcha = (user==nil || even_if_logged_in) && Preference.get_cached(self.system_id, "use_captcha")=='true' && (risk > self.use_captcha_above_risk) logger.debug "** Use Text Captcha? #{user==nil ? 'Not logged in user' : 'User'}, Country #{country ? country.name : 'unknown'} Risk #{country ? country.risk : 'unknown'}, Form Risk #{self.use_captcha_above_risk}, Even if logged in #{even_if_logged_in}, Use Captcha #{Preference.get(self.system_id, 'use_captcha')} Using Captcha: #{use_captcha}" return use_captcha end |