Skip to content

Commit

Permalink
Can list and create segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doug Youch committed May 24, 2010
1 parent 0101309 commit 6c813ab
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 38 deletions.
62 changes: 45 additions & 17 deletions app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ class MembersController < CmsController # :nodoc: all
hdr(:static, 'Profile', :width => '70'),
hdr(:static, 'Image', :width => '70'),
'Name',
'Email',
hdr(:static, 'Src. Type', :label => 'Src. Type'),
hdr(:static, 'Src.', :label => 'Src.'),
hdr(:static, 'Created', :width => '120'),
hdr(:static, 'Reg.', :label => 'Reg.', :width => '120')
'Email'
],
:count_callback => 'count_end_users',
:find_callback => 'find_end_users'
Expand All @@ -32,7 +28,10 @@ class MembersController < CmsController # :nodoc: all


def segmentations
@segmentations ||= UserSegment.find(:all, :conditions => {:main_page => true}, :order => 'name')
return @segmentations if @segmentations
@segmentations = UserSegment.find(:all, :conditions => {:main_page => true}, :order => 'name')
@segmentations << self.segment if self.segment && ! @segmentations.find { |seg| seg.id == self.segment.id }
@segmentations
end

def segment
Expand All @@ -41,12 +40,7 @@ def segment

def count_end_users(opts)
if self.search_results
pages, users = self.search_results
if self.search.user_segment
self.search.offsets.length * self.search.per_page - (self.search.per_page - users.length)
else
pages[:total]
end
self.search.total
elsif self.segment
self.segment.last_count
else
Expand All @@ -56,10 +50,9 @@ def count_end_users(opts)

def find_end_users(opts)
if self.search_results
pages, users = self.search_results
users
self.search.users
elsif self.segment
pages, users = self.segment.paginate self.search.page, :per_page => 25
pages, users = self.segment.paginate self.search.page, :per_page => 25, :include => [:user_class, :domain_file]
users
else
EndUser.find(:all, :conditions => 'client_user_id IS NULL', :include => [:user_class, :domain_file], :offset => opts[:offset], :limit => opts[:limit], :order => opts[:order])
Expand All @@ -81,7 +74,7 @@ def search
end

def search_results
@search_results ||= self.search.search if self.search.terms
@search_results ||= self.search.search(:include => [:user_class, :domain_file]) if self.search.terms
end

def get_user_classes
Expand Down Expand Up @@ -170,7 +163,42 @@ def index

display_targets_table(false)
end


active_table :user_segments_table,
UserSegment,
[ hdr(:icon, 'check', :width => '16'),
:main_page,
:name,
:description,
:last_count,
:last_ran_at,
:created_at
]

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

def segments
cms_page_path ['People'], 'Segments'
user_segments_table(false)
end

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

@segment = UserSegment.new params[:segment]

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

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

Expand Down
26 changes: 19 additions & 7 deletions app/models/end_user_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ def search(opts={})
scope = EndUserCache.search self.terms
if self.user_segment
self.offsets << self.offset unless self.offsets.include?(self.offset)
new_offset, users = self.user_segment.search opts.merge(:scope => scope, :offset => self.offset, :limit => self.per_page)
if users.length >= self.per_page
self.offsets << (new_offset+1) unless self.offsets.include?(new_offset+1)
@new_offset, @users = self.user_segment.search opts.merge(:scope => scope, :offset => self.offset, :limit => self.per_page)
if @users.length >= self.per_page
self.offsets << (@new_offset+1) unless self.offsets.include?(@new_offset+1)
end
[new_offset, users]
[@new_offset, @users]
else
pages, ids = scope.paginate(self.page, :select => 'end_user_id')
@pages, ids = scope.paginate(self.page, :select => 'end_user_id')
ids = ids.collect { |cache| cache[:end_user_id] }
users_by_id = EndUser.find(:all, opts.merge(:conditions => {:id => ids})).index_by(&:id)
users = ids.map { |id| users_by_id[id] }
[pages, users]
@users = ids.map { |id| users_by_id[id] }
[@pages, @users]
end
end

def total
if self.user_segment
self.offsets.length * self.per_page - (self.per_page - @users.length)
else
@pages[:total]
end
end

def users
@users
end
end
8 changes: 0 additions & 8 deletions app/views/members/_targets_table.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
t.editor? ? h(email) : content_tag(:a, email, {:href => url_for(:action => 'edit', :path => t.id)})
%>
</td>
<td align='center'><%= h @source_options[t.source] %></td>
<td align='center'><%= h t.lead_source.blank? ? '-' : t.lead_source %></td>
<td align='center'>
<%= t.created_at ? t.created_at.strftime(DEFAULT_DATE_FORMAT.t) : '-' %>
</td>
<td align='center'>
<%= t.registered_at ? t.registered_at.strftime(DEFAULT_DATE_FORMAT.t) : '-' %>
</td>
</tr>

<% end -%>
15 changes: 15 additions & 0 deletions app/views/members/_user_segments_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<% active_table_for :user_segments_table, @active_table_output,
:class => 'active_table',
:width => '100%',
:more_actions => [ ['Delete Segments','delete','Delete the selected segments?'] ],
:update => 'user_segments_table' do |t| %>
<tr <%= highlight_row "user",t.id %> >
<td nowrap='1'><%= entry_checkbox 'user',t.id %></td>
<td align='center'><%= t.main_page ? 'Yes'.t : 'No'.t %></td>
<td nowrap='1'><%= h t.name %></td>
<td><%= h(t.description ? t.description : '') %></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.created_at.strftime(DEFAULT_DATETIME_FORMAT.t) %></td>
</tr>
<% end -%>
9 changes: 9 additions & 0 deletions app/views/members/create_segment.html.erb
Original file line number Diff line number Diff line change
@@ -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', 'Create' %>
<% end -%>
35 changes: 29 additions & 6 deletions app/views/members/index.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@

tableAction: function(action,params) {
ActiveTable.action(action,null,"email_targets_table","<%= url_for :action => 'display_targets_table' %>",'targets_table',params);
},

changeSegments: function() {
id = $('user_segments_user_segment_id').value;
window.location = id ? "/website/members/index/" + id : "/website/members"
}


}
</script>

Expand All @@ -102,18 +105,34 @@
<%= p.link "Download Visible Targets",:icon => 'download.gif', :controller => :member_export %>
<%= p.link "Tag List", :icon => 'show.gif', :action => 'tags' %>
<%= p.link "Access Tokens", :icon => 'access.gif', :controller => '/access_token' if myself.has_role?('editor_access_tokens') %>
<%= p.link "Segments", :icon => 'access.gif', :action => 'segments' %>
<% end -%>

<hr/>

<div class="member_search">
<div class="user_segments" style="float:right; width: 300px; text-align:right; padding:5px;">
<%= form_tag %>
<%= text_field_tag :terms, @search.terms %>
<%= submit_tag 'Search' %>
<% if @segment && @segment.last_ran_at -%>
<a href="#">Edit</a> | <a href="#">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/>
<% if @segment && @segment.last_ran_at -%>
<span style="font-size:11px;"><%= 'updated on %s' / @segment.last_ran_at.strftime(DEFAULT_DATETIME_FORMAT.t) %></span>
<br/>
<% end -%>
<a href="<%= url_for :action => 'create_segment' %>">Create a Segment</a>
</form>
</div>

<div class="member_search" style="padding:5px; width:320px;">
<%= form_tag %>
<%= text_field_tag :terms, @search.terms, :id => 'members_search_terms' %>
<%= submit_tag 'Search'.t %>
</form>
</div>
<hr/>

<hr style="clear:both;"/>

<script>
<%= active_table_javascript %>
Expand All @@ -124,3 +143,7 @@


</div>

<script>
$('members_search_terms').focus();
</script>
9 changes: 9 additions & 0 deletions app/views/members/segments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
<%= active_table_javascript %>
</script>

<br />

<div id='user_segments_table'>
<%= render :partial => 'user_segments_table' %>
</div>

0 comments on commit 6c813ab

Please sign in to comment.