lib/ahoy/model.rb in ahoy_matey-0.1.4 vs lib/ahoy/model.rb in ahoy_matey-0.1.5

- old
+ new

@@ -19,16 +19,12 @@ self.search_keyword = search_keyword.presence if respond_to?(:search_keyword=) true end def set_utm_parameters - landing_uri = Addressable::URI.parse(landing_page) rescue nil - if landing_uri - query_values = landing_uri.query_values || {} - %w[utm_source utm_medium utm_term utm_content utm_campaign].each do |name| - self[name] = query_values[name] if respond_to?(:"#{name}=") - end + %w[utm_source utm_medium utm_term utm_content utm_campaign].each do |name| + self[name] = landing_params[name] if respond_to?(:"#{name}=") end true end def set_technology @@ -90,26 +86,34 @@ end end true end + def landing_params + @landing_params ||= begin + landing_uri = Addressable::URI.parse(landing_page) rescue nil + ActiveSupport::HashWithIndifferentAccess.new((landing_uri && landing_uri.query_values) || {}) + end + end + end # end class_eval end - def visitable + def visitable(name = nil, options = {}) + if name.is_a?(Hash) + name = nil + options = name + end + name ||= :visit class_eval do - belongs_to :visit - + belongs_to name, options before_create :set_visit - + end + class_eval %Q{ def set_visit - if !self.class.column_names.include?("visit_id") - raise "Add a visit_id column to this table to use visitable" - else - self.visit ||= RequestStore.store[:ahoy_controller].try(:send, :current_visit) - end + self.#{name} ||= RequestStore.store[:ahoy_controller].try(:send, :current_visit) end - end + } end end end