Module: Googletastic::Helpers::FormModelHelper
- Defined in:
- lib/googletastic/helpers/form.rb
Overview
was Googletastic::Helpers::Form, but rails through fits with namespacing problems
Class Method Summary collapse
-
.included(base) ⇒ Object
class options for Form: as: property name for the googletastic form foreign_key: foreign key to reference it, defaults to "as"_id sync: the properties you want to sync with the form redirect_to: where to redirect the forms submission form_only: don't include title and header that google gives you (defaults to false).
Class Method Details
.included(base) ⇒ Object
class options for Form:
as: property name for the googletastic form
foreign_key: foreign key to reference it, defaults to "as"_id
sync: the properties you want to sync with the form
redirect_to: where to redirect the forms submission
form_only: don't include title and header that google gives you (defaults to false)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 |
# File 'lib/googletastic/helpers/form.rb', line 13 def self.included(base) # defaults = Googletastic[base] [:as] ||= "google_form" [:foreign_key] ||= "#{options[:as]}_id" [:form_key] ||= "form_key" [:form_only] = false unless .has_key?(:form_only) if .has_key?(:sync) [:sync] = case [:sync].class.to_s when "Symbol" {[:sync] => [:sync]} when "String" {[:sync].to_sym => [:sync].to_sym} when "Array" [:sync].collect { |v| {v.to_sym => v.to_sym} } else [:sync].symbolize_keys end end # fast access as = [:as] foreign_key = [:foreign_key] sync = [:sync] form_key = [:form_key] # eval base.class_eval " attr_accessor :\#{as}\n attr_accessor :content\n \n def self.find_with_\#{as}(*args)\n google_records = Googletastic::Form.all\n foreign_keys = google_records.collect { |record| record.id }\n records = find_all_by_\#{foreign_key}(foreign_keys) || []\n record_keys = records.collect { |record| record.\#{foreign_key} }\n google_records.each do |google_record|\n if !record_keys.include?(google_record.id)\n record = self.new(\n :\#{foreign_key} => google_record.id,\n :updated_at => google_record.updated_at,\n :\#{as} => google_record,\n :title => google_record.title\n )\n record.save\n records << record\n end\n end\n \n records.each do |record|\n record.\#{as} = google_records.select { |r| r.id == record.\#{foreign_key} }.first\n end\n records\n end\n \n # get form content\n def get\n self.\#{as}.form_key ||= self.\#{form_key}\n self.\#{as}.get\n end\n \n if base.is_a?(ActiveRecord::Base)\n \n before_validation :clean_form_key\n validates_presence_of :\#{as}\n validates_uniqueness_of :\#{as}\n validate :validate_formkey_is_valid\n before_save :sync_with_google\n\n def sync_with_google\n Hash(\#{sync}).each do |mine, theirs|\n self[mine] = self.\#{as}[theirs]\n end if Googletastic[self].has_key?(:sync)\n end\n\n def validate_form_key_is_valid\n case self.\#{as}.get\n when Net::HTTPSuccess\n true\n else\n errors.add(:form_key, \"is not a valid Google Forms key or URL or error connecting to Google\")\n false\n end\n end\n\n end\n end_eval\nend\n", __FILE__, __LINE__ |