Skip to content

Instantly share code, notes, and snippets.

@dawidof
Last active October 19, 2015 10:02
Show Gist options
  • Save dawidof/7f9f248e3faef6a537be to your computer and use it in GitHub Desktop.
Save dawidof/7f9f248e3faef6a537be to your computer and use it in GitHub Desktop.
=simple_form_for @inbox_sent, url: compose_send_inboxes_path, method: :post, html: {id: 'fileupload', class: 'inbox-compose form-horizontal', multipart: true} do |f|
.inbox-compose-btn
=button_tag id: 'send_message', class: 'btn blue'
i.fa.fa-check
| Send
=button_tag "Cancel", id: 'discard_changes', class: 'btn'
=button_tag "Draft", id: 'save_draft', class: 'btn'
.inbox-form-group.mail-to
= f.input :to, wrapper: false do
.controls.controls-to
=f.input_field :to, class: 'form-control'
span.inbox-cc-bcc
span.inbox-cc Cc
span.inbox-bcc Bcc
.inbox-form-group.input-cc.display-hide
= f.input :cc, wrapper: false do
=link_to "", "javascript:;", class: 'close'
.controls.controls-to
=f.input_field :cc, class: 'form-control'
.inbox-form-group.input-bcc.display-hide
= f.input :bcc, wrapper: false do
=link_to "", "javascript:;", class: 'close'
.controls.controls-to
=f.input_field :bcc, class: 'form-control'
.inbox-form-group
= f.input :subject, wrapper: false do
.controls
=f.input_field :subject, class: 'form-control'
.inbox-form-group
= f.input :message_getaway_id, wrapper: false do
.controls
=f.select :message_getaway_id, options_for_select(MessageGetaway.to_send.map { |mg| [mg.email + ' - ' + MessageGetaway::TYPES.zip(MessageGetaway::TYPES_RUS)[mg.type_mask][1], mg.id] }), {}, {class: 'form-control'}
.inbox-form-group
= f.input :body, wrapper: false, label: false do
=f.input_field :body, class: ' inbox-editor inbox-wysihtml5 form-control', rows: '12'
=simple_fields_for :sent_attachments do |p|
=f.input :is_draft, as: :hidden
.inbox-compose-attachment
span.btn.green.fileinput-button
i.fa.fa-plus
span Add files...
=p.input :file, as: :file, input_html: {:multiple => true}, label: false
table.table.table-striped.margin-top-10 role="presentation"
tbody.files
=render 'file_templates'
.inbox-compose-btn
=button_tag id: 'send_message', class: 'btn blue'
i.fa.fa-check
| Send
=button_tag "Cancel", id: 'discard_changes', class: 'btn'
=button_tag "Draft", id: 'save_draft', class: 'btn'
Started POST "/inboxes/compose_send" for ::1 at 2015-10-19 11:48:03 +0200
Processing by InboxesController#compose_send as */*
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Sh13I5GqMrEgxRCT7dLFr4AHgqQSy0RS3Jfv2ctN5oecaONqm6VCsbEZuHG5pNlv9UmHU1XsyJc3k9j7meTgIw==", "inbox_sent"=>{"to"=>"dawidof@some.com", "cc"=>"", "bcc"=>"", "subject"=>"Test message", "message_getaway_id"=>"4", "body"=>"<pre><code>name = \"my text\"\r\nx.times do name.chop! end</code></pre><p>Here in the console:</p><pre><code>&gt;name = \"Nabucodonosor\"\r\n =&gt; \"Nabucodonosor\" \r\n&gt; 7.times do name.chop! end\r\n =&gt; 7 \r\n&gt; name\r\n =&gt; \"Nabuco\" \r\n<img src=\"http://static1.squarespace.com/static/55d3b229e4b0ee67516b60df/t/55d69f5de4b07fd45af08ee9/1440128862762/business.jpg?format=1500w\" title=\"Image: http://static1.squarespace.com/static/55d3b229e4b0ee67516b60df/t/55d69f5de4b07fd45af08ee9/1440128862762/business.jpg?format=1500w\">\r\n</code></pre>", "is_draft"=>"false"}, "_wysihtml5_mode"=>"1", "sent_attachments"=>{"file"=>[#<ActionDispatch::Http::UploadedFile:0x007fd076a6d288 @tempfile=#<Tempfile:/var/folders/c9/fzm03ldx5kg1_5y2nywmdw300000gn/T/RackMultipart20151019-41019-18xw1ib.jpg>, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"sent_attachments[file][]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fd076a6d260 @tempfile=#<Tempfile:/var/folders/c9/fzm03ldx5kg1_5y2nywmdw300000gn/T/RackMultipart20151019-41019-169oeii.jpg>, @original_filename="2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"sent_attachments[file][]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
class InboxesController < ApplicationController
### actions
def compose_send
@sent_message = SentMessage.new(sent_message_params)
@sent_message.project_id = cookies[:project_id]
@sent_message.draft = false unless @sent_message.is_draft
if @sent_message.is_draft
if @sent_message.save
if params[:sent_attachments].present?
params[:sent_attachments]['file'].each do |a|
@sent_attachment = @sent_message.sent_attachments.create!(:file => a, :sent_message => @sent_message)
end
end
@notification = 'Message saved in draft!'
@alert_type = 'success'
render "compose_send", status: 200
else
@notification = 'Error during saving'
@alert_type = 'danger'
render "compose_send", status: 500
end
else
if @sent_message.to.blank? or @sent_message.subject.blank? or @sent_message.body.blank? or @sent_message.project_id.blank? or @sent_message.message_getaway_id.blank?
@notification = '<b>Please fill in:!</b><br>'
@alert_type = 'danger'
@notification += '<b>To</b><br>' if @sent_message.to.blank?
@notification += '<b>Subject</b><br>' if @sent_message.subject.blank?
@notification += '<b>Body</b><br>' if @sent_message.body.blank?
render "compose_send", status: 500
elsif @sent_message.save
if params[:sent_attachments].present?
params[:sent_attachments]['file'].each do |a|
@sent_message_attachment = @sent_message.sent_attachments.create!(:file => a, :sent_message => @sent_message)
end
end
@notification = 'Message sent!'
@alert_type = 'success'
render "compose_send", status: 200
else
@notification = 'Error during saving!'
@alert_type = 'danger'
render "compose_send", status: 500
end
end
end
private
def sent_message_params
params.require(:sent_message).permit(:to, :from_email, :from_name, :cc, :bcc, :subject, :body, :message_getaway_id, :is_draft, sent_attachments_attributes: [:file])
end
end
require 'message_sender'
class SentMessage < ActiveRecord::Base
include MessageSender
belongs_to :message_getaway
belongs_to :project
has_many :sent_attachments, dependent: :destroy
accepts_nested_attributes_for :sent_attachments
after_commit :send_current_message
def is_draft
@is_draft ||= false
end
def is_draft=(val)
@is_draft = val == 'true'
end
def send_current_message
puts 'send_message'
if (!draft and !body.include?('/email/open/') and !body.include?('/email/t/') )
puts 'is not draft'
self.send_this_message #method in ext. file
end
end
before_save :check_values
def check_values
if to.present? or
from_email.present? or
from_name.present? or
cc.present? or
bcc.present? or
subject.present? or
body.present? or
message_getaway.present? or
inbox_sent_attachments.size > 0
self.showed = true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment