From 7ad065fda0a0a6d2c79be499f2b8072c2f83e8e6 Mon Sep 17 00:00:00 2001 From: keith-ferney Date: Mon, 20 Aug 2018 11:46:15 -0600 Subject: [PATCH] should be able to start workflow for period and cycle and sequence should be removed from periods --- app/controllers/admin/periods_controller.rb | 2 +- app/controllers/documents_controller.rb | 2 +- app/controllers/organizations_controller.rb | 21 ++++++++++++------- app/controllers/periods_controller.rb | 2 +- .../workflow_documents_controller.rb | 2 +- app/mailers/workflow_mailer.rb | 14 ++++++------- app/models/period.rb | 6 ++++++ app/views/admin/periods/_form.html.erb | 12 ----------- app/views/admin/periods/index.html.erb | 6 ++---- app/views/admin/periods/show.html.erb | 4 ---- app/views/components/_edit.html.erb | 8 +++---- .../start_workflow_form.html.erb | 5 +++++ app/views/periods/_form.html.erb | 12 ----------- app/views/periods/index.html.erb | 4 ---- app/views/periods/show.html.erb | 4 ---- ..._remove_cycle_and_sequence_from_periods.rb | 6 ++++++ db/schema.rb | 4 +--- .../previews/workflow_mailer_preview.rb | 16 ++++++++------ 18 files changed, 59 insertions(+), 71 deletions(-) create mode 100644 db/migrate/20180820162331_remove_cycle_and_sequence_from_periods.rb diff --git a/app/controllers/admin/periods_controller.rb b/app/controllers/admin/periods_controller.rb index fd6c253b..d6ac3fff 100644 --- a/app/controllers/admin/periods_controller.rb +++ b/app/controllers/admin/periods_controller.rb @@ -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 diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 90ddce13..0687cf8e 100755 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -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 diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 0de62998..849a60c5 100755 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -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 diff --git a/app/controllers/periods_controller.rb b/app/controllers/periods_controller.rb index 34b34b2d..7c587387 100644 --- a/app/controllers/periods_controller.rb +++ b/app/controllers/periods_controller.rb @@ -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 diff --git a/app/controllers/workflow_documents_controller.rb b/app/controllers/workflow_documents_controller.rb index 19548799..bd4a275d 100644 --- a/app/controllers/workflow_documents_controller.rb +++ b/app/controllers/workflow_documents_controller.rb @@ -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 diff --git a/app/mailers/workflow_mailer.rb b/app/mailers/workflow_mailer.rb index f9ab9d2e..08531a92 100644 --- a/app/mailers/workflow_mailer.rb +++ b/app/mailers/workflow_mailer.rb @@ -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) diff --git a/app/models/period.rb b/app/models/period.rb index 7bf018d4..5da68023 100644 --- a/app/models/period.rb +++ b/app/models/period.rb @@ -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 diff --git a/app/views/admin/periods/_form.html.erb b/app/views/admin/periods/_form.html.erb index 4a3932b0..f2c6a174 100644 --- a/app/views/admin/periods/_form.html.erb +++ b/app/views/admin/periods/_form.html.erb @@ -38,18 +38,6 @@ <%= f.number_field :duration, class: 'form-control input-xxlarge' %> -
- <%= f.label :cycle, class: "control-label" %> -
- <%= f.text_field :cycle, class: 'form-control input-xxlarge' %> -
-
-
- <%= f.label :sequence, class: "control-label" %> -
- <%= f.number_field :sequence, class: 'form-control input-xxlarge' %> -
-
<%= f.label :is_default, class: "control-label" %> <%= f.check_box :is_default, class: '' %> diff --git a/app/views/admin/periods/index.html.erb b/app/views/admin/periods/index.html.erb index 38fa944b..fe1d76f4 100644 --- a/app/views/admin/periods/index.html.erb +++ b/app/views/admin/periods/index.html.erb @@ -9,9 +9,8 @@ <%= model_class.human_attribute_name(:slug) %> <%= model_class.human_attribute_name(:organization) %> <%= model_class.human_attribute_name(:start_date) %> + <%= model_class.human_attribute_name(:end_date) %> <%= model_class.human_attribute_name(:duration) %> - <%= model_class.human_attribute_name(:cycle) %> - <%= model_class.human_attribute_name(:sequence) %> <%= model_class.human_attribute_name(:is_default) %> <%=t '.actions', :default => t("helpers.actions") %> @@ -24,9 +23,8 @@ <%= period.slug %> <%= period.organization.slug if period.organization_id %> <%= period.start_date %> + <%= period.end_date %> <%= period.duration.to_s + " days" if period.duration%> - <%= period.cycle %> - <%= period.sequence %> <%= period.is_default %> <%= link_to t('.edit', :default => t("helpers.links.edit")), edit_admin_period_path(period.id), :class => 'btn btn-default btn-xs' %> diff --git a/app/views/admin/periods/show.html.erb b/app/views/admin/periods/show.html.erb index 51ffefca..c7cbc84b 100644 --- a/app/views/admin/periods/show.html.erb +++ b/app/views/admin/periods/show.html.erb @@ -15,10 +15,6 @@
<%= @period.start_date %>
<%= model_class.human_attribute_name(:duration) %>:
<%= @period.duration.to_s + " days" if @period.duration %>
-
<%= model_class.human_attribute_name(:cycle) %>:
-
<%= @period.cycle %>
-
<%= model_class.human_attribute_name(:sequence) %>:
-
<%= @period.sequence %>
<%= model_class.human_attribute_name(:is_default) %>:
<%= @period.is_default %>
diff --git a/app/views/components/_edit.html.erb b/app/views/components/_edit.html.erb index 756005e4..b2f2ad20 100755 --- a/app/views/components/_edit.html.erb +++ b/app/views/components/_edit.html.erb @@ -46,7 +46,7 @@
- <% if @available_component_formats.include? 'erb' %> + <% if @organization.enable_workflows %>
@@ -83,9 +83,9 @@
when using liquid format refer to Liquid guide for designers
- 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 <%= @available_liquid_variables.keys.join(', ') %> + and for the user_welcome_email there is the user_activation_url variable
+ and for the workflow_welcome_email and step_emails there is the document_url variable
diff --git a/app/views/organizations/start_workflow_form.html.erb b/app/views/organizations/start_workflow_form.html.erb index fe81cfe3..7564df43 100644 --- a/app/views/organizations/start_workflow_form.html.erb +++ b/app/views/organizations/start_workflow_form.html.erb @@ -21,6 +21,11 @@
+
+ <%= f.label :start_for_sub_organizations, class: "control-label" %> + <%= f.check_box :start_for_sub_organizations, checked: true,class:"btn" %> +
+
<%= f.submit "Start Workflow for period", class: 'btn btn-default' %>
diff --git a/app/views/periods/_form.html.erb b/app/views/periods/_form.html.erb index edf4b270..16f0202a 100644 --- a/app/views/periods/_form.html.erb +++ b/app/views/periods/_form.html.erb @@ -34,18 +34,6 @@ <%= f.number_field :duration, class: 'form-control input-xxlarge' %> -
- <%= f.label :cycle, class: "control-label" %> -
- <%= f.text_field :cycle, class: 'form-control input-xxlarge' %> -
-
-
- <%= f.label :sequence, class: "control-label" %> -
- <%= f.number_field :sequence, class: 'form-control input-xxlarge' %> -
-
<%= f.label :is_default, class: "control-label" %> <%= f.check_box :is_default, class: '' %> diff --git a/app/views/periods/index.html.erb b/app/views/periods/index.html.erb index f79b911c..5711d58a 100644 --- a/app/views/periods/index.html.erb +++ b/app/views/periods/index.html.erb @@ -10,8 +10,6 @@ <%= model_class.human_attribute_name(:organization) %> <%= model_class.human_attribute_name(:start_date) %> <%= model_class.human_attribute_name(:duration) %> - <%= model_class.human_attribute_name(:cycle) %> - <%= model_class.human_attribute_name(:sequence) %> <%= model_class.human_attribute_name(:is_default) %> <%=t '.actions', :default => t("helpers.actions") %> @@ -25,8 +23,6 @@ <%= period.organization.slug if period.organization_id%> <%= period.start_date %> <%= period.duration.to_s + " days" if period.duration %> - <%= period.cycle %> - <%= period.sequence %> <%= period.is_default %> <%= link_to t('.edit', :default => t("helpers.links.edit")), edit_period_path(params[:slug],period.id), :class => 'btn btn-default btn-xs' %> diff --git a/app/views/periods/show.html.erb b/app/views/periods/show.html.erb index be975538..39b574ce 100644 --- a/app/views/periods/show.html.erb +++ b/app/views/periods/show.html.erb @@ -15,10 +15,6 @@
<%= @period.start_date %>
<%= model_class.human_attribute_name(:duration) %>:
<%= @period.duration.to_s + " days" if @period.duration %>
-
<%= model_class.human_attribute_name(:cycle) %>:
-
<%= @period.cycle %>
-
<%= model_class.human_attribute_name(:sequence) %>:
-
<%= @period.sequence %>
<%= model_class.human_attribute_name(:is_default) %>:
<%= @period.is_default %>
diff --git a/db/migrate/20180820162331_remove_cycle_and_sequence_from_periods.rb b/db/migrate/20180820162331_remove_cycle_and_sequence_from_periods.rb new file mode 100644 index 00000000..accbac41 --- /dev/null +++ b/db/migrate/20180820162331_remove_cycle_and_sequence_from_periods.rb @@ -0,0 +1,6 @@ +class RemoveCycleAndSequenceFromPeriods < ActiveRecord::Migration[5.1] + def change + remove_column :periods, :sequence, :integer + remove_column :periods, :cycle, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 387e664a..d2415c3d 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" diff --git a/spec/mailers/previews/workflow_mailer_preview.rb b/spec/mailers/previews/workflow_mailer_preview.rb index 8e601785..0af9208d 100644 --- a/spec/mailers/previews/workflow_mailer_preview.rb +++ b/spec/mailers/previews/workflow_mailer_preview.rb @@ -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