-
Notifications
You must be signed in to change notification settings - Fork 41
/
content_model_segment_field.rb
93 lines (79 loc) · 3.78 KB
/
content_model_segment_field.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
class ContentModelSegmentField < UserSegment::FieldHandler
def self.user_segment_fields_handler_info
{
:name => self.content_model.name,
:domain_model_class => self.content_model.content_model,
:end_user_field => self.end_user_field
}
end
def self.sing
class << self; self; end
end
class OptionsType < UserSegment::FieldType
def self.is(cls, group_field, field, string)
cls.scoped(:conditions => ["#{field} = ?", string])
end
def self.create_type(options)
cls = Class.new OptionsType
cls.register_operation :is, [['Option', :option, {:options => options}]]
cls
end
end
class MultiOptionsType < UserSegment::FieldType
def self.is(cls, group_field, field, string)
cls.scoped(:conditions => ["#{field} like ?", "%#{string}%"])
end
def self.create_type(options)
cls = Class.new MultiOptionsType
cls.register_operation :is, [['Option', :option, {:options => options}]]
cls
end
end
def self.create_custom_field_handler_class(content_model_field)
content_model = content_model_field.content_model
cls = Class.new ContentModelSegmentField
cls.sing.send :define_method, :content_model do
content_model
end
cls.sing.send :define_method, :end_user_field do
content_model_field.field.to_sym
end
content_model.content_model_fields.each do |field|
case field.field_type
when 'string'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::StringType, :field => field.field, :name => field.name
when 'email'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::StringType, :field => field.field, :name => field.name
when 'boolean'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::BooleanType, :field => field.field, :name => field.name
when 'date'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::DateTimeType, :field => field.field, :name => field.name
when 'datetime'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::DateTimeType, :field => field.field, :name => field.name
when 'integer'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::NumberType, :field => field.field, :name => field.name
when 'currency'
cls.register_field "#{content_model.table_name}_#{field.field}", UserSegment::CoreType::NumberType, :field => field.field, :name => field.name
when 'options'
type = ContentModelSegmentField::OptionsType.create_type field.module_class.available_options.clone
cls.register_field "#{content_model.table_name}_#{field.field}", type, :field => field.field, :name => field.name
when 'multi_select'
type = ContentModelSegmentField::MultiOptionsType.create_type field.module_class.available_options.clone
cls.register_field "#{content_model.table_name}_#{field.field}", type, :field => field.field, :name => field.name
when 'us_state'
type = ContentModelSegmentField::OptionsType.create_type Content::CoreField::UsStateField.states_select_options
cls.register_field "#{content_model.table_name}_#{field.field}", type, :field => field.field, :name => field.name
end
end
cls
end
def self.field_heading(field)
self.content_model.name + ' - ' + self.user_segment_fields[field][:name]
end
def self.get_handler_data(ids, fields)
self.content_model.content_model.find(:all, :conditions => {self.end_user_field => ids}).group_by(&self.end_user_field)
end
def self.field_output(user, handler_data, field)
UserSegment::FieldType.field_output(user, handler_data, field)
end
end