Skip to content

Commit

Permalink
Merge pull request idbygeorge#126 from keith-ferney/stage
Browse files Browse the repository at this point in the history
should be able to start workflow for period and cycle and sequence
  • Loading branch information
keith-ferney committed Aug 20, 2018
2 parents 92bb7ed + 7ad065f commit d3e5a4d
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 71 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/periods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ def update

private
def period_params
params.require(:period).permit(:name, :slug, :organization_id, :start_date, :duration, :cycle, :sequence, :is_default)
params.require(:period).permit(:name, :slug, :organization_id, :start_date, :duration, :is_default)
end
end
2 changes: 1 addition & 1 deletion app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def update
end
if params[:publish] == "true" && @organization.enable_workflows && user
if @document.workflow_step_id && @document.assigned_to?(user)
WorkflowMailer.step_email(user, @organization, @document.workflow_step.slug, component_allowed_liquid_variables(@document.workflow_step, user,@organization)).deliver_later
WorkflowMailer.step_email(@document, request.env["SERVER_NAME"],user, @organization, @document.workflow_step.slug, component_allowed_liquid_variables(@document.workflow_step, user,@organization)).deliver_later
@document.workflow_step_id = @document.workflow_step.next_workflow_step_id if @document.workflow_step&.next_workflow_step_id
@document.save!
end
Expand Down
21 changes: 14 additions & 7 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,29 @@ def start_workflow_form
end

def start_workflow
params.require("Start Workflow").permit(:document_name,:starting_workflow_step_id,:period_id)
params.require("Start Workflow").permit(:document_name,:starting_workflow_step_id,:period_id,:start_for_sub_organizations)
start_workflow_params = params["Start Workflow"]
if start_workflow_params[:period_id] == "" || start_workflow_params[:starting_workflow_step_id] == "" || start_workflow_params[:document_name] == ""
flash[:error] = "all fields must be filled"
return redirect_back(fallback_location: start_workflow_form_path)
end
organization = get_org
user_ids = organization.user_assignments.where(role:"staff").map(&:user_id)
users = User.where(id: user_ids)
users.each do |user|
document = Document.create(name: start_workflow_params[:document_name], workflow_step_id: start_workflow_params[:starting_workflow_step_id].to_i, organization_id: organization.id, period_id: start_workflow_params[:period_id].to_i, user_id: user.id)
WorkflowMailer.welcome_email(user,organization,document.workflow_step.slug,component_allowed_liquid_variables(document.workflow_step.slug, user, organization)).deliver_later
if start_workflow_params[:start_for_sub_organizations]
organizations = organization.children + [organization]
else
organizations = [organization]
end
organizations.each do |org|
user_ids = org.user_assignments.where(role:"staff").map(&:user_id)
users = User.where(id: user_ids)
users.each do |user|
document = Document.create(name: start_workflow_params[:document_name], workflow_step_id: start_workflow_params[:starting_workflow_step_id].to_i, organization_id: org.id, period_id: start_workflow_params[:period_id].to_i, user_id: user.id)
WorkflowMailer.welcome_email(document, request.env["SERVER_NAME"], user, org, document.workflow_step.slug,component_allowed_liquid_variables(document.workflow_step.slug, user, org)).deliver_later
end
end

flash[:notice] = "successfully started workflow for period"
return redirect_back(fallback_location: start_workflow_form_path)
return redirect_to start_workflow_form_path
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/periods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ def find_or_create_period


def period_params
params.require(:period).permit(:name, :slug, :start_date, :duration, :cycle, :sequence, :is_default)
params.require(:period).permit(:name, :slug, :start_date, :duration, :is_default)
end
end
2 changes: 1 addition & 1 deletion app/controllers/workflow_documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def update
@wfs = WorkflowStep.find(params[:document][:workflow_step_id])
if @wfs.step_type == "start_step"
@user = User.find(params[:document][:user_id])
WorkflowMailer.welcome_email(@user,@organization,@wfs.slug,component_allowed_liquid_variables(@document.workflow_step,User.find(params[:document][:user_id]),@organization)).deliver_later
WorkflowMailer.welcome_email(@document, request.env["SERVER_NAME"], @user, @organization, @wfs.slug, component_allowed_liquid_variables(@document.workflow_step,User.find(params[:document][:user_id]),@organization)).deliver_later
end
end

Expand Down
14 changes: 7 additions & 7 deletions app/mailers/workflow_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
class WorkflowMailer < ApplicationMailer
def step_email user, organization, step_slug, allowed_variables
def step_email document, host, user, organization, step_slug, allowed_variables
orgs = organization.parents.push(organization)
allowed_variables["workflow_documents_url"] = workflow_document_index_url(host: organization.slug)
allowed_variables["document_url"] = document_url(document.view_id, host: host)
workflow_step = WorkflowStep.find_by(organization_id: orgs.map(&:id), slug: step_slug)
@mail_component = Component.find_by(organization_id: orgs.map(&:id), category: "mailer", slug: "#{step_slug}_mailer", format: "liquid")
@mail_component = Component.find_by(organization_id: orgs.map(&:id), category: "mailer", slug: "#{step_slug}_email", format: "liquid")
@next_component = Component.find_by(organization_id: orgs.map(&:id), slug: WorkflowStep.find_by(organization_id: orgs.map(&:id), id:workflow_step&.next_workflow_step_id).slug) if workflow_step&.next_workflow_step_id
if @mail_component
@template = Liquid::Template.parse(@mail_component.layout)
@subject = Liquid::Template.parse(@mail_component.subject).render(allowed_variables).html_safe
@step_email = @template.render(allowed_variables).html_safe
if @next_component && @next_component.role == "supervisor"
orgs = organization.parents.push(organization).select { |org| org.level.to_i == @next_component&.role_organization_level.to_i }
user = UserAssignment.find_by(organization_id: orgs.map(&:id) ,role:"supervisor").user
supervisor_orgs = organization.parents.push(organization).sort_by { |o| o["level"] }.select { |org| org.level.to_i <= @next_component&.role_organization_level.to_i }
user = UserAssignment.find_by(organization_id: supervisor_orgs.map(&:id) ,role:"supervisor")&.user
end
mail(to: user.email, subject: @subject)
end
end

def welcome_email user, organization, step_slug, allowed_variables
def welcome_email document, host, user, organization, step_slug, allowed_variables
orgs = organization.parents.push(organization)
allowed_variables["workflow_documents_url"] = workflow_document_index_url(host: organization.slug)
allowed_variables["document_url"] = document_url(document.view_id, host: host)
@mail_component = Component.find_by(organization_id: orgs.map(&:id),category: "mailer", slug: "workflow_welcome_email", format: "liquid")
if @mail_component
@template = Liquid::Template.parse(@mail_component.layout)
Expand Down
6 changes: 6 additions & 0 deletions app/models/period.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class Period < ApplicationRecord
validates :slug, presence: true
validates :start_date, presence: true
validates :duration, presence: true
validates :slug, uniqueness: { scope: :organization }
validates_uniqueness_of :is_default, if: :is_default, scope: :organization, message: "is already set for this organization"

belongs_to :organization
has_many :documents

def end_date
self.start_date + self.duration.days if self.start_date && self.duration
end
end
12 changes: 0 additions & 12 deletions app/views/admin/periods/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@
<%= f.number_field :duration, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :cycle, class: "control-label" %>
<div class="controls">
<%= f.text_field :cycle, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :sequence, class: "control-label" %>
<div class="controls">
<%= f.number_field :sequence, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :is_default, class: "control-label" %>
<%= f.check_box :is_default, class: '' %>
Expand Down
6 changes: 2 additions & 4 deletions app/views/admin/periods/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
<th><%= model_class.human_attribute_name(:slug) %></th>
<th><%= model_class.human_attribute_name(:organization) %></th>
<th><%= model_class.human_attribute_name(:start_date) %></th>
<th><%= model_class.human_attribute_name(:end_date) %></th>
<th><%= model_class.human_attribute_name(:duration) %></th>
<th><%= model_class.human_attribute_name(:cycle) %></th>
<th><%= model_class.human_attribute_name(:sequence) %></th>
<th><%= model_class.human_attribute_name(:is_default) %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
Expand All @@ -24,9 +23,8 @@
<td><%= period.slug %></td>
<td><%= period.organization.slug if period.organization_id %></td>
<td><%= period.start_date %></td>
<td><%= period.end_date %></td>
<td><%= period.duration.to_s + " days" if period.duration%></td>
<td><%= period.cycle %></td>
<td><%= period.sequence %></td>
<td><%= period.is_default %></td>
<td><%= link_to t('.edit', :default => t("helpers.links.edit")), edit_admin_period_path(period.id), :class => 'btn btn-default btn-xs' %></td>
</tr>
Expand Down
4 changes: 0 additions & 4 deletions app/views/admin/periods/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<dd><%= @period.start_date %></dd>
<dt><strong><%= model_class.human_attribute_name(:duration) %>:</strong></dt>
<dd><%= @period.duration.to_s + " days" if @period.duration %></dd>
<dt><strong><%= model_class.human_attribute_name(:cycle) %>:</strong></dt>
<dd><%= @period.cycle %></dd>
<dt><strong><%= model_class.human_attribute_name(:sequence) %>:</strong></dt>
<dd><%= @period.sequence %></dd>
<dt><strong><%= model_class.human_attribute_name(:is_default) %>:</strong></dt>
<dd><%= @period.is_default %></dd
</dl>
Expand Down
8 changes: 4 additions & 4 deletions app/views/components/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>
</div>

<% if @available_component_formats.include? 'erb' %>
<% if @organization.enable_workflows %>
<div class="row">
<div class="col-md-6">
<div class="form-group">
Expand Down Expand Up @@ -83,9 +83,9 @@
<div class="help-block">
when using liquid format refer to <a href="https://github.com/Shopify/liquid/wiki/Liquid-for-Designers" target="_blank">Liquid guide for designers</a>
<br>
and the available variables are <%= @available_liquid_variables.keys.join(', ') %>
and for the user_welcome_email there is the user_activation_url variable
and for the workflow_welcome_email there is the workflow_documents_url variable
and the available variables are <b><%= @available_liquid_variables.keys.join(', ') %></b>
and for the user_welcome_email there is the <b>user_activation_url</b> variable<br>
and for the workflow_welcome_email and step_emails there is the <b>document_url</b> variable
</div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions app/views/organizations/start_workflow_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
</div>
</div>

<div class="form-group">
<%= f.label :start_for_sub_organizations, class: "control-label" %>
<%= f.check_box :start_for_sub_organizations, checked: true,class:"btn" %>
</div>

<div class="form-group">
<%= f.submit "Start Workflow for period", class: 'btn btn-default' %>
</div>
Expand Down
12 changes: 0 additions & 12 deletions app/views/periods/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@
<%= f.number_field :duration, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :cycle, class: "control-label" %>
<div class="controls">
<%= f.text_field :cycle, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :sequence, class: "control-label" %>
<div class="controls">
<%= f.number_field :sequence, class: 'form-control input-xxlarge' %>
</div>
</div>
<div class="form-group">
<%= f.label :is_default, class: "control-label" %>
<%= f.check_box :is_default, class: '' %>
Expand Down
4 changes: 0 additions & 4 deletions app/views/periods/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
<th><%= model_class.human_attribute_name(:organization) %></th>
<th><%= model_class.human_attribute_name(:start_date) %></th>
<th><%= model_class.human_attribute_name(:duration) %></th>
<th><%= model_class.human_attribute_name(:cycle) %></th>
<th><%= model_class.human_attribute_name(:sequence) %></th>
<th><%= model_class.human_attribute_name(:is_default) %></th>
<th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
Expand All @@ -25,8 +23,6 @@
<td><%= period.organization.slug if period.organization_id%></td>
<td><%= period.start_date %></td>
<td><%= period.duration.to_s + " days" if period.duration %></td>
<td><%= period.cycle %></td>
<td><%= period.sequence %></td>
<td><%= period.is_default %></td>
<td><%= link_to t('.edit', :default => t("helpers.links.edit")), edit_period_path(params[:slug],period.id), :class => 'btn btn-default btn-xs' %></td>
</tr>
Expand Down
4 changes: 0 additions & 4 deletions app/views/periods/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
<dd><%= @period.start_date %></dd>
<dt><strong><%= model_class.human_attribute_name(:duration) %>:</strong></dt>
<dd><%= @period.duration.to_s + " days" if @period.duration %></dd>
<dt><strong><%= model_class.human_attribute_name(:cycle) %>:</strong></dt>
<dd><%= @period.cycle %></dd>
<dt><strong><%= model_class.human_attribute_name(:sequence) %>:</strong></dt>
<dd><%= @period.sequence %></dd>
<dt><strong><%= model_class.human_attribute_name(:is_default) %>:</strong></dt>
<dd><%= @period.is_default %></dd
</dl>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RemoveCycleAndSequenceFromPeriods < ActiveRecord::Migration[5.1]
def change
remove_column :periods, :sequence, :integer
remove_column :periods, :cycle, :string
end
end
4 changes: 1 addition & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180816213409) do
ActiveRecord::Schema.define(version: 20180820162331) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -144,8 +144,6 @@
t.string "name"
t.integer "organization_id"
t.integer "duration"
t.string "cycle"
t.integer "sequence"
t.boolean "is_default"
t.datetime "created_at"
t.datetime "updated_at"
Expand Down
16 changes: 10 additions & 6 deletions spec/mailers/previews/workflow_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
# Preview all emails at http://localhost:3000/rails/mailers/workflow_mailer
class WorkflowMailerPreview < ActionMailer::Preview
def step_email
org = Organization.first
org = Organization.find(2)
orgs = org.parents.push(org)
user = User.first
step_slug = WorkflowStep.where(organization_id: orgs.map(&:id)).first.slug
wfs = WorkflowStep.where(organization_id: orgs.map(&:id)).first
step_slug = wfs.slug
doc = Document.create(user_id: user.id, organization_id: orgs.map(&:id),workflow_step_id: wfs.id)
allowed_variables = {"user_name" => "#{user&.name}","user_email" => "#{user&.email}", "organization_name" => "#{org&.name}", "step_slug" => "#{step_slug}"}
WorkflowMailer.step_email(user,org,step_slug, allowed_variables)
WorkflowMailer.step_email(doc, org.slug, user,org,step_slug, allowed_variables)
end

def welcome_email
org = Organization.first
org = Organization.find(2)
orgs = org.parents.push(org)
user = User.first
step_slug = WorkflowStep.where(organization_id: orgs.map(&:id)).first.slug
wfs = WorkflowStep.where(organization_id: orgs.map(&:id)).first
step_slug = wfs.slug
doc = Document.create(user_id: user.id, organization_id: orgs.map(&:id),workflow_step_id: wfs.id)
allowed_variables = {"user_name" => "#{user&.name}","user_email" => "#{user&.email}", "organization_name" => "#{org&.name}", "step_slug" => "#{step_slug}"}
WorkflowMailer.welcome_email(user,org,step_slug, allowed_variables)
WorkflowMailer.welcome_email(doc, org.slug, user,org,step_slug, allowed_variables)
end
end

0 comments on commit d3e5a4d

Please sign in to comment.