Skip to content

Commit

Permalink
Added status to user segment to keep track of where in the process of
Browse files Browse the repository at this point in the history
calculating end user ids it is.  Added edit segment and refresh segement
to members controller.
  • Loading branch information
Doug Youch committed May 20, 2010
1 parent aae4b1c commit 385b7da
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
35 changes: 29 additions & 6 deletions app/controllers/members_controller.rb
Expand Up @@ -7,7 +7,8 @@ class MembersController < CmsController # :nodoc: all
layout 'manage'

cms_admin_paths "people",
"People" => { :controller => '/members' }
"People" => { :controller => '/members' },
"Segments" => { :action => 'segments' }


# need to include
Expand All @@ -29,7 +30,7 @@ class MembersController < CmsController # :nodoc: all

def segmentations
return @segmentations if @segmentations
@segmentations = UserSegment.find(:all, :conditions => {:main_page => true}, :order => 'name')
@segmentations = UserSegment.find(:all, :conditions => {:main_page => true, :status => 'finished'}, :order => 'name')
@segmentations << self.segment if self.segment && ! @segmentations.find { |seg| seg.id == self.segment.id }
@segmentations
end
Expand Down Expand Up @@ -167,16 +168,19 @@ def index
active_table :user_segments_table,
UserSegment,
[ hdr(:icon, 'check', :width => '16'),
hdr(:icon, '', :width => '32'),
:main_page,
:name,
:description,
:status,
:last_count,
:last_ran_at,
:created_at
:created_at,
:updated_at
]

def user_segments_table(display=true)
@active_table_output = user_segments_table_generate params, :order => 'created_at DESC'
@active_table_output = user_segments_table_generate params, :order => 'updated_at DESC'
render :partial => 'user_segments_table' if display
end

Expand All @@ -186,19 +190,38 @@ def segments
end

def create_segment
cms_page_path ['People'], 'Create a Segment'
cms_page_path ['People', 'Segments'], 'Create a Segment'

@segment = UserSegment.new params[:segment]

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

def edit_segment
@segment = UserSegment.find params[:path][0]

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

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

def refresh_segment
@segment = UserSegment.find params[:path][0]
@segment.refresh
redirect_to :action => 'segments'
end

def create
cms_page_path ['People'],'Create Contact'

Expand Down
33 changes: 31 additions & 2 deletions app/models/user_segment.rb
@@ -1,11 +1,18 @@

class UserSegment < DomainModel

has_many :user_segment_caches, :order => 'position', :dependent => :destroy, :class_name => 'UserSegmentCache'
has_many :user_segment_caches, :order => 'position', :dependent => :delete_all, :class_name => 'UserSegmentCache'
serialize :segment_options
serialize :fields

validates_presence_of :name
validates_presence_of :segment_options

def ready?; self.status == 'finished'; end

def validate
self.errors.add(:segment_options_text, 'is invalid') if self.segment_options_text && self.segment_options.nil?
end

def operations
return @operations if @operations
Expand All @@ -15,14 +22,31 @@ def operations
end

def segment_options_text=(text)
text = text.gsub("\r", '').strip
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 cache_ids
def refresh
if self.status == 'new' || self.ready?
self.status = 'refreshing'
self.save

if EndUser.count < 50000
self.cache_ids
else
self.run_worker(:cache_ids)
end
end
end

def cache_ids(opts={})
self.status = 'calculating'
self.save

self.user_segment_caches.delete_all

self.order_by = 'created_at DESC' unless self.order_by
Expand All @@ -36,6 +60,7 @@ def cache_ids
self.user_segment_caches.create :id_list => ids[start..start+UserSegmentCache::SIZE-1], :position => idx
end

self.status = 'finished'
self.last_ran_at = Time.now
self.last_count = ids.length
self.save
Expand Down Expand Up @@ -137,5 +162,9 @@ def search(args={})
users = EndUser.find(:all, args.merge(:conditions => {:id => ids}))
return [offset, users]
end

def before_create
self.status = 'new'
end
end

17 changes: 15 additions & 2 deletions app/views/members/_user_segments_table.html.erb
Expand Up @@ -5,11 +5,24 @@
:update => 'user_segments_table' do |t| %>
<tr <%= highlight_row "user",t.id %> >
<td nowrap='1'><%= entry_checkbox 'user',t.id %></td>
<td align='center' nowrap='1'>
<% if t.ready? -%>
<a href="<%= url_for :action => 'edit_segment', :path => t.id %>"><%= theme_image_tag('icons/actions/edit.gif') %></a>
<% end -%>
</td>
<td align='center'><%= t.main_page ? 'Yes'.t : 'No'.t %></td>
<td nowrap='1'><%= h t.name %></td>
<td nowrap='1'>
<% if t.ready? -%>
<a href="<%= url_for :action => 'index', :path => t.id %>"><%= h t.name %></a>
<% else -%>
<%= t.name %>
<% end -%>
</td>
<td><%= h(t.description ? t.description : '') %></td>
<td align='center'><%= t.status %></td>
<td align='center'><%= t.last_count %></td>
<td align='center'><%= t.last_ran_at.strftime(DEFAULT_DATETIME_FORMAT.t) %></td>
<td align='center'><%= t.last_ran_at.strftime(DEFAULT_DATETIME_FORMAT.t) if t.last_ran_at %></td>
<td align='center'><%= t.created_at.strftime(DEFAULT_DATETIME_FORMAT.t) %></td>
<td align='center'><%= t.updated_at.strftime(DEFAULT_DATETIME_FORMAT.t) %></td>
</tr>
<% end -%>
9 changes: 9 additions & 0 deletions app/views/members/edit_segment.html.erb
@@ -0,0 +1,9 @@
<% admin_form_for :segment, @segment do |f| -%>
<%= f.text_field :name %>
<%= f.text_area :description, :rows => 3 %>
<%= f.check_boxes :main_page, [['display on people page', true]], :single => true %>
<%= f.text_area :segment_options_text, :rows => 8, :label => 'Operations' %>
<%= f.spacer %>
<%= f.cancel_submit_buttons 'Cancel', 'Save' %>
<% end -%>
2 changes: 1 addition & 1 deletion app/views/members/index.rhtml
Expand Up @@ -113,7 +113,7 @@
<div class="user_segments" style="float:right; width: 300px; text-align:right; padding:5px;">
<%= form_tag %>
<% if @segment && @segment.last_ran_at -%>
<a href="#">Edit</a> | <a href="#">Refresh</a>
<a href="<%= url_for :action => 'edit_segment', :path => @segment.id %>">Edit</a> | <a href="<%= url_for :action => 'refresh_segment', :path => @segment.id %>">Refresh</a>
<% end -%>
<%= select_tag :user_segment_id, options_for_select([['Everyone', nil]] + @segmentations.collect { |seg| [seg.name, seg.id] }, (@segment ? @segment.id : nil)), :onchange => 'MemberEditor.changeSegments();', :id => 'user_segments_user_segment_id', :style => 'width:200px;' %>
<br/>
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20100511165552_user_segments.rb
Expand Up @@ -3,7 +3,7 @@ def self.up
create_table :user_segments, :force => true do |t|
t.string :name
t.text :description
t.string :type
t.string :status
t.boolean :main_page
t.datetime :last_ran_at
t.integer :last_count
Expand Down

0 comments on commit 385b7da

Please sign in to comment.