Skip to content
Browse files

Implement email controller and model

  • Loading branch information...
1 parent 1de9ba9 commit a2765a55f58169838b385f1897cb1c10112f54d7 @Sutto Sutto committed Apr 29, 2010
View
16 app/controllers/admin/emails_controller.rb
@@ -0,0 +1,16 @@
+class Admin::EmailsController < AdminController
+
+ def new
+ @email = Email.new
+ end
+
+ def create
+ @email = Email.new(params[:email])
+ if @email.save
+ redirect_to :new_admin_email, :notice => "Email sent - thanks!"
+ else
+ render :action => "new"
+ end
+ end
+
+end
View
95 app/models/email.rb
@@ -0,0 +1,95 @@
+class Email
+ include ActiveModel::Conversion
+ include ActiveModel::AttributeMethods
+ include ActiveModel::Validations
+ extend ActiveModel::Naming
+
+ SCOPE_TYPES = [["All Users", "users"], ["Filtered Participations", "participations"]]
+
+ ASSIGNABLE_ATTRIBUTES = [:subject, :html_content, :text_content, :scope_type, :filter]
+ attr_accessor *ASSIGNABLE_ATTRIBUTES
+ define_attribute_methods ASSIGNABLE_ATTRIBUTES
+
+ validates_presence_of :subject
+ validates_inclusion_of :scope_type, :in => SCOPE_TYPES.map(&:last)
+ validate :has_atleast_one_content_type
+ validate :has_atleast_one_user
+
+ def initialize(attributes = {})
+ self.attributes = attributes
+ end
+
+ def attributes=(attributes)
+ return unless attributes.is_a?(Hash)
+ attributes.symbolize_keys.each_pair do |k, v|
+ send :"#{k}=", v if ASSIGNABLE_ATTRIBUTES.include? k.to_sym
+ end
+ end
+
+ def filter
+ @filter ||= OpenStruct.new
+ end
+
+ def filter=(value)
+ if value.blank? || !value.is_a?(Hash)
+ @filter = OpenStruct.new
+ else
+ @filter = OpenStruct.new(value)
+ @filter.mission_id = @filter.mission_id.to_i if @filter.mission_id.present?
+ end
+ end
+
+ def save
+ valid? && send_email
+ end
+
+ def persisted?
+ false
+ end
+
+ def send_email
+ Rails.logger.debug "==================="
+ Rails.logger.debug "Sending to #{user_scope.count} users"
+ Rails.logger.debug "Users: #{user_scope.all.inspect}"
+ Rails.logger.debug "Subject: #{subject}"
+ Rails.logger.debug "-- Text Contents --"
+ Rails.logger.debug text_content.to_s
+ Rails.logger.debug "-- HTML Contents --"
+ Rails.logger.debug html_content.to_s
+ Rails.logger.debug "==================="
+ end
+
+ def self.mapping_to_scope(name, filter)
+ name = name.to_s
+ scope = User.unscoped
+ scope = scope.where('id IN (?)', build_id_list(filter)) if name == "participations"
+ scope
+ end
+
+ def self.build_id_list(filter)
+ scope = MissionParticipation.unscoped
+ scope = scope.where('mission_id = ?', filter.mission_id.to_i) if filter.mission_id.present?
+ scope = scope.only_role(filter.role) if filter.role.present?
+ scope = scope.with_states(filter.states) if filter.states.present?
+ scope = scope.from_pickups(filter.pickups) if filter.pickups.present?
+ scope.select(:user_id).all.map(&:user_id).uniq
+ end
+
+ protected
+
+ def user_scope
+ self.class.mapping_to_scope(scope_type, filter)
+ end
+
+ def has_atleast_one_content_type
+ if html_content.blank? && text_content.blank?
+ errors.add :html_content, "at least one content section must be filled in"
+ errors.add :text_content, "at least one content section must be filled in"
+ end
+ end
+
+ def has_atleast_one_user
+ errors.add_to_base "There must be atleast one user" if user_scope.size < 1
+ end
+
+end
View
4 app/models/mission.rb
@@ -102,6 +102,10 @@ def normalize_friendly_id(text)
def unstarted?
%w(preparing approved).include?(self.state)
end
+
+ def self.for_select
+ select('name, id').all.map { |m| [m.name, m.id] }
+ end
end
View
35 app/views/admin/emails/new.html.haml
@@ -0,0 +1,35 @@
+- has_jammit_js :ckeditor
+
+%h2.title Send a new email
+
+= semantic_form_for @email, :url => :admin_emails do |form|
+
+ = form.inputs :name => "Email Details" do
+
+ = form.input :subject
+
+ = form.input :scope_type, :collection => Email::SCOPE_TYPES, :as => :select, :include_blank => false
+
+ = form.inputs :name => "Filter Participations", :id => "participations_filter" do
+ = form.fields_for :filter, @email.filter do |ff|
+
+ = ff.input :mission_id, :as => :select, :collection => Mission.for_select, :include_blank => true
+
+ = ff.input :role, :as => :select, :collection => ParticipationReporter::ROLE_CHOICES, :include_blank => false
+
+ = ff.input :states, :as => :select, :collection => ParticipationReporter::STATE_CHOICES, :multiple => true, :include_blank => false
+
+
+ = form.inputs :name => "Email Content" do
+
+ = form.input :text_content, :as => :text, :input_html => {:rows => 15}
+
+ = form.input :html_content, :as => :text, :input_html => {:rows => 15}, :wrapper_html => {:class => 'ckeditor'}
+
+ = form.buttons do
+ = form.commit_button "Send Email"
+
+- content_for :sidebar do
+
+ %h2 Menu
+ %ul.sidebar-menu= ml "Send a new Email", :new_admin_email
View
1 config/routes.rb
@@ -1,6 +1,7 @@
Bighelpmob::Application.routes.draw do |map|
namespace :admin do
+ resources :emails, :only => [:new, :create]
resources :users do
resources :mission_participations, :path => 'participations'
end

0 comments on commit a2765a5

Please sign in to comment.
Something went wrong with that request. Please try again.