Skip to content

Commit

Permalink
Can copy a segment. Dynamically display user fields based on
Browse files Browse the repository at this point in the history
segmentation.
  • Loading branch information
Doug Youch committed May 24, 2010
1 parent 17c0255 commit 43cfcfc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
54 changes: 39 additions & 15 deletions app/controllers/members_controller.rb
Expand Up @@ -12,21 +12,9 @@ class MembersController < CmsController # :nodoc: all


# need to include
include ActiveTable::Controller
active_table :email_targets_table,
EndUser,
[ hdr(:icon, 'check', :width => '16'),
hdr(:static, 'Profile', :width => '70'),
hdr(:static, 'Image', :width => '70'),
'Name',
'Email'
],
:count_callback => 'count_end_users',
:find_callback => 'find_end_users'

include ActiveTable::Controller

protected


def segmentations
return @segmentations if @segmentations
Expand Down Expand Up @@ -107,6 +95,25 @@ def handle_table_actions
end
end

def email_targets_table_generate(opts,*find_options)
@generated_active_table_columns = [
ActiveTable::IconHeader.new('check', :width => '16'),
ActiveTable::StaticHeader.new('Profile', :width => '70'),
ActiveTable::StaticHeader.new('Image', :width => '70'),
ActiveTable::StaticHeader.new('Name'),
ActiveTable::StaticHeader.new('Email')
]

if self.segment && self.segment.fields
self.segment.fields.each do |field|
option = UserSegment.fields_options.rassoc(field)
@generated_active_table_columns << ActiveTable::StaticHeader.new(option[1], :label => option[0]) if option
end
end

active_table_generate('end_users', EndUser, @generated_active_table_columns, {:count_callback => 'count_end_users', :find_callback => 'find_end_users'}, opts, *find_options)
end

public

def lookup_autocomplete
Expand All @@ -132,7 +139,6 @@ def lookup_autocomplete
render :partial => 'lookup_autocomplete'
end


def display_targets_table(display = true)

if params[:table_action] || params[:segment_action]
Expand All @@ -154,7 +160,9 @@ def display_targets_table(display = true)
# Members editing
def index
cms_page_path [], "People"


return redirect_to(:action => 'segments') if self.segment && ! self.segment.ready?

segmentations

display_targets_table(false)
Expand Down Expand Up @@ -212,6 +220,22 @@ def edit_segment

cms_page_path ['People', 'Segments'], '%s Segment' / @segment.name

if request.post? && params[:segment]
if @segment.update_attributes params[:segment]
@segment.refresh if @segment.should_refresh?
redirect_to :action => 'segments'
end
end
end

def copy_segment
segment_to_copy = UserSegment.find params[:path][0]

cms_page_path ['People', 'Segments'], 'Copy %s Segment' / segment_to_copy.name

@segment = UserSegment.new segment_to_copy.attributes.symbolize_keys.slice(:name, :description, :fields, :main_page, :segment_options_text, :order_by)
@segment.name += ' (Copy)' unless request.post?

if request.post? && params[:segment]
if @segment.update_attributes params[:segment]
@segment.refresh
Expand Down
7 changes: 6 additions & 1 deletion app/models/user_segment.rb
Expand Up @@ -28,13 +28,18 @@ def operations

def segment_options_text=(text)
text = text.gsub("\r", '').strip
@should_refresh = self.segment_options_text != text
self.write_attribute :segment_options_text, text
@operations = UserSegment::Operations.new
@operations.parse text
self.segment_options = @operations.valid? ? self.operations.to_a : nil
text
end

def should_refresh?
@should_refresh
end

def refresh
if self.status == 'new' || self.ready?
self.status = 'refreshing'
Expand Down Expand Up @@ -173,7 +178,7 @@ def before_create
end

def self.fields_options
[['Source', 'source'], ['Date of Birth', 'dob'], ['Gender', 'gender'], ['Created', 'created_at'], ['Registered', 'registered_at']]
[['Source', 'source'], ['Date of Birth', 'dob'], ['Gender', 'gender'], ['Created', 'created_at'], ['Registered', 'registered_at'], ['User Class', 'user_class']]
end

def self.order_by_options
Expand Down
33 changes: 20 additions & 13 deletions app/views/members/_targets_table.rhtml
@@ -1,22 +1,12 @@
<% @user_classes = []
@user_level_options = EndUser.user_level_select_options
@source_options = EndUser.source_options_hash
@view = "View".t
@edit = "Edit".t
@delete = "Delete".t
@user_class_objs = UserClass.find(:all)
@user_class_index = @user_class_objs.index_by(&:id)
@user_class_objs.each { |cls| @user_classes[cls.id] = h(cls.name.t) } %>
<% active_table_for :email_targets_table, @active_table_output,
:refresh_url => url_for(:action => 'display_targets_table', :path => @segment ? @segment.id : nil, :terms => @search.terms, :offsets => @search.offsets.empty? ? nil : @search.offsets),
:class => 'active_table',
:width => '100%',
:next => @search.user_segment && @search.terms ? 'more' : '&gt;',
:actions => [ ['Add Tags','js','MemberEditor.addTags();'],
['Remove Tags','js','MemberEditor.removeTags();']
:actions => [['Add Tags','js','MemberEditor.addTags();'],
['Remove Tags','js','MemberEditor.removeTags();']
],
:more_actions => [ ['Delete Targets','delete','Delete the selected users?'] ],
:more_actions => [['Delete Targets','delete','Delete the selected users?']],
:update => 'targets_table' do |t| %>
<tr <%= highlight_row "user",t.id %> >
<td nowrap='1'>
Expand All @@ -36,6 +26,23 @@
t.editor? ? h(email) : content_tag(:a, email, {:href => url_for(:action => 'edit', :path => t.id)})
%>
</td>

<% if @segment && @segment.fields -%>
<% @segment.fields.each do |field| -%>
<%=
value = t.send(field)
if value.is_a?(DomainModel)
"<td align='center'>#{value.name}</td>"
elsif value.is_a?(Time)
"<td align='center'>#{value.strftime(DEFAULT_DATETIME_FORMAT.t)}</td>"
elsif value.nil?
"<td align='center'>-</td>"
else
"<td align='center'>#{value}</td>"
end
%>
<% end -%>
<% end -%>
</tr>

<% end -%>
1 change: 1 addition & 0 deletions app/views/members/copy_segment.html.erb
@@ -0,0 +1 @@
<%= render :partial => 'segment_form' %>

0 comments on commit 43cfcfc

Please sign in to comment.