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] ||= "#{[: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 <<-end_eval, __FILE__, __LINE__ attr_accessor :#{as} attr_accessor :content def self.find_with_#{as}(*args) google_records = Googletastic::Form.all foreign_keys = google_records.collect { |record| record.id } records = find_all_by_#{foreign_key}(foreign_keys) || [] record_keys = records.collect { |record| record.#{foreign_key} } google_records.each do |google_record| if !record_keys.include?(google_record.id) record = self.new( :#{foreign_key} => google_record.id, :updated_at => google_record.updated_at, :#{as} => google_record, :title => google_record.title ) record.save records << record end end records.each do |record| record.#{as} = google_records.select { |r| r.id == record.#{foreign_key} }.first end records end # get form content def get self.#{as}.form_key ||= self.#{form_key} self.#{as}.get end if base.is_a?(ActiveRecord::Base) before_validation :clean_form_key validates_presence_of :#{as} validates_uniqueness_of :#{as} validate :validate_formkey_is_valid before_save :sync_with_google def sync_with_google Hash(#{sync}).each do |mine, theirs| self[mine] = self.#{as}[theirs] end if Googletastic[self].has_key?(:sync) end def validate_form_key_is_valid case self.#{as}.get when Net::HTTPSuccess true else errors.add(:form_key, "is not a valid Google Forms key or URL or error connecting to Google") false end end end end_eval end |