Skip to content

Commit

Permalink
Added module level options for fields to display and ordering of the
Browse files Browse the repository at this point in the history
everyone view.
  • Loading branch information
Doug Youch committed May 20, 2010
1 parent 6d7528f commit 108b0b0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
54 changes: 47 additions & 7 deletions app/controllers/members_controller.rb
Expand Up @@ -104,11 +104,16 @@ def email_targets_table_generate(opts,*find_options)
ActiveTable::StaticHeader.new('Email')
]

@fields = []
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
@fields = self.segment.fields
else self.class.module_options && self.class.module_options.fields
@fields = self.class.module_options.fields
end

@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

active_table_generate('end_users', EndUser, @generated_active_table_columns, {:count_callback => 'count_end_users', :find_callback => 'find_end_users'}, opts, *find_options)
Expand Down Expand Up @@ -145,7 +150,7 @@ def display_targets_table(display = true)
handle_table_actions
end

@active_table_output = email_targets_table_generate params, :per_page => 25, :include => :tag_cache, :conditions => 'client_user_id IS NULL', :order => 'created_at DESC'
@active_table_output = email_targets_table_generate params, :per_page => 25, :include => :tag_cache, :conditions => 'client_user_id IS NULL', :order => self.class.module_options.order_by

if display
@update_tags = true
Expand Down Expand Up @@ -197,6 +202,37 @@ def user_segments_table(display=true)
render :partial => 'user_segments_table' if display
end

def options
cms_page_path ['People'], "Everyone Options"

@options = self.class.module_options(params[:options])

if request.post? && @options.valid?
Configuration.set_config_model(@options)
flash[:notice] = "Updated Everyone options".t
redirect_to :action => 'index'
return
end
end

def self.module_options(vals=nil)
Configuration.get_config_model(Options,vals)
end

class Options < HashModel
attributes :fields => [], :order_by => 'created_at DESC'

def validate
if self.fields
self.errors.add(:fields, 'is invalid') if self.fields.find { |f| UserSegment.fields_options.rassoc(f).nil? }
end

if self.order_by
self.errors.add(:order_by, 'is invalid') unless UserSegment.order_by_options.rassoc(self.order_by)
end
end
end

def segments
cms_page_path ['People'], 'Segments'
user_segments_table(false)
Expand All @@ -222,8 +258,12 @@ def edit_segment

if request.post? && params[:segment]
if @segment.update_attributes params[:segment]
@segment.refresh if @segment.should_refresh?
redirect_to :action => 'segments'
if @segment.should_refresh?
@segment.refresh
redirect_to :action => 'segments'
else
redirect_to :action => 'index', :path => @segment.id
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/user_segment.rb
Expand Up @@ -36,6 +36,12 @@ def segment_options_text=(text)
text
end

def order_by=(order)
@should_refresh = self.order_by != order
self.write_attribute :order_by, order
order
end

def should_refresh?
@should_refresh
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/members/_targets_table.rhtml
Expand Up @@ -27,8 +27,8 @@
%>
</td>

<% if @segment && @segment.fields -%>
<% @segment.fields.each do |field| -%>
<% if @fields -%>
<% @fields.each do |field| -%>
<%=
value = t.send(field)
if value.is_a?(DomainModel)
Expand Down
2 changes: 2 additions & 0 deletions app/views/members/index.rhtml
Expand Up @@ -114,6 +114,8 @@
<%= form_tag %>
<% if @segment && @segment.last_ran_at -%>
<a href="<%= url_for :action => 'edit_segment', :path => @segment.id %>">Edit</a> | <a href="<%= url_for :action => 'refresh_segment', :path => @segment.id %>">Refresh</a>
<% else -%>
<a href="<%= url_for :action => 'options' %>">Edit</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
7 changes: 7 additions & 0 deletions app/views/members/options.html.erb
@@ -0,0 +1,7 @@
<% admin_form_for :options, @options do |f| -%>
<%= f.select :order_by, UserSegment.order_by_options %>
<%= f.ordered_array :fields, UserSegment.fields_options, :label => 'Fields to Display' %>
<%= f.spacer %>
<%= f.cancel_submit_buttons 'Cancel', 'Save' %>
<% end -%>

0 comments on commit 108b0b0

Please sign in to comment.