Skip to content

Commit

Permalink
Added end user action field types
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed May 21, 2010
1 parent ff70675 commit 87f0703
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/controllers/module_controller.rb
Expand Up @@ -78,6 +78,7 @@ def self.get_module_handlers

def self.register_handler(component,handler,class_name,options={})
component = component.to_sym; handler = handler.to_sym
options[:handler] = handler
handlers = self.get_module_handlers
handlers[component] ||= {}
handlers[component][handler] ||= []
Expand Down
2 changes: 1 addition & 1 deletion app/models/end_user_action_segment_field.rb
Expand Up @@ -9,7 +9,7 @@ def self.user_segment_fields_handler_info
end

register_field :renderer, UserSegment::CoreType::StringType
register_field :action, UserSegment::CoreType::StringType
register_field :action, EndUserActionSegmentType::ActionType
register_field :created, UserSegment::CoreType::DateTimeType, :field => :created_at
register_field :occurred, UserSegment::CoreType::DateTimeType, :field => :action_at

Expand Down
17 changes: 17 additions & 0 deletions app/models/end_user_action_segment_type.rb
@@ -0,0 +1,17 @@

class EndUserActionSegmentType

class ActionType < UserSegment::FieldType
include HandlerActions

def self.action_options
self.get_handlers(:action).collect { |handler| [handler[1][:description], handler[1][:handler].to_s] }
end

register_operation :is, [['Action', :option, {:options => EndUserActionSegmentType::ActionType.action_options}]]

def self.is(cls, field, action)
cls.scoped(:conditions => ["#{field} = ?", action])
end
end
end
13 changes: 8 additions & 5 deletions app/models/handler_actions.rb
Expand Up @@ -7,7 +7,7 @@ def self.append_features(base) #:nodoc:
base.extend(ClassMethods)
end

def get_handlers(component,handler,initialized=false)
def get_handlers(component,handler=nil,initialized=false)
self.class.get_handlers(component,handler,initialized)
end

Expand Down Expand Up @@ -40,8 +40,7 @@ def get_handler_instance(component,handler_name,identifier,*initialize_args)

module ClassMethods

def get_handlers(component,handler,initialized = false)
component = component.to_sym; handler = handler.to_sym
def get_handlers(component,handler=nil,initialized = false)
handlers = DataCache.get_cached_container("Handlers","Active") unless initialized || RAILS_ENV != 'production'
unless handlers
mods = initialized ? SiteModule.initialized_modules_info : SiteModule.enabled_modules_info
Expand All @@ -68,10 +67,14 @@ def get_handlers(component,handler,initialized = false)
end
DataCache.put_container("Handlers","Active",handlers) unless initialized || RAILS_ENV == 'development'
end
return (handlers[component]||{})[handler] || []

if handler
(handlers[component.to_sym]||{})[handler.to_sym] || []
else
(handlers[component.to_sym] || {}).values.inject([]) { |a,b| a.concat(b) }
end
end

def get_handler_options(component,handler_name,initialized=false)
handlers = get_handlers(component,handler_name,initialized)
handlers.collect do |handler|
Expand Down
12 changes: 11 additions & 1 deletion app/models/user_segment/field_type.rb
Expand Up @@ -42,6 +42,8 @@ def self.register_operation(operation, args=[], options={})
# converts a string to the correct type
# supported types are :integer, :float, :double, :date, :datetime, :option, :boolean
def self.convert_to(value, type, opts={})
return value if value.nil?

case type
when :integer
return value.to_i if value.is_a?(Integer) || value =~ /^\d+$/
Expand All @@ -56,7 +58,15 @@ def self.convert_to(value, type, opts={})
rescue
end
when :option
value = opts[:options].find { |o| o.downcase == value.downcase }
value = opts[:options].find do |o|
if o.is_a?(Array)
o[1].downcase == value.downcase
else
o.downcase == value.downcase
end
end

return value[1] if value.is_a?(Array)
return value if value
when :boolean
return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
Expand Down

0 comments on commit 87f0703

Please sign in to comment.