Skip to content

Commit

Permalink
Remove company number from membership application, restructure copy<>…
Browse files Browse the repository at this point in the history
…application associations (AgileVentures#486)

* migration done, seeding works, started model work

* progress on model specs

* rspec controller tests

* interim progress - cuke tests

* working on cuke tests and UI view changes

* working on errors exposed by cuke tests

* more changes arising from tests

* more work on cuke test changes

* rspec test changes

* fixed cuke test

* adjusted shf update action logic

* removed cmpy destroy when app is rejected

* fixed app update action when no company_number in form

* changed FactoryGirl to FactoryBot

* fixed error introduced by develop merge

* refactoring app controller update action

* fixed cmpy-delete cuke tests

* fixed app update action logic

* baseline for restructuring created app association with cmpy

* app create/update allow select cmpy_number (or create cmpy)

* adjusted rspec tests

* interim progress on cuke tests

* finished cuke tests changes

* fixed some hound issues

* changed logic to prevent exposing company numbers to user

* adjusted styling

* fixed seeds.rb and rspect tests

* adjusted app-form styling for new-co link

* add id to orgnum entry to tie to field label

* adjusted cuke tests for orgnummer entry (vs. selection)

* fixed helper spec test

* restructure cuke tests size_validation

* added page reload in cuke test

* added wait in cuke test

* added ajax-wait steps to cuke test

* added page reload - cuke test

* undo prior changes to cuke test

* restructure cuke test

* added wait in cuke test

* added exc handling to retry on "stale element" exception

* changed form references to IDs to avoid ambiguous fill_in error

* adjusted migrations and seeding

* adjusted migrate logic after reviewing prod data migration

* adjusted migrate logic

* more adjusts to migration logic

* merged from develop

* refactored cmpys JS to reduce flaky cuke test likelihood

* modified db schema with migration
  • Loading branch information
patmbolger authored Mar 23, 2018
1 parent 6f171ed commit f49d7ae
Show file tree
Hide file tree
Showing 66 changed files with 1,177 additions and 640 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/companies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ $(function() {
$('#editBrandingStatusSubmit').click(function() {
$('#edit-branding-modal').modal('hide');
});

$('#companyCreateForm').on('ajax:success', function (e, data) {
$('#' + data.id).html(data.html);
if (data.status === 'success') {
$('#company-create-modal').modal('hide');
$('#company-create-errors').html('');
}
});
});
11 changes: 11 additions & 0 deletions app/assets/stylesheets/shf-applications.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ $app-state-border-blue: rgb(142, 173, 206);
margin-bottom: 0;
}

.new-company-link {
display: inline-block;
margin-left: 30px;
vertical-align: middle;
}

}
}

#company-number-entry {
display: inline-block;
width: 30%;
}

#supporting-files {

margin-bottom: 1em;
Expand Down
26 changes: 22 additions & 4 deletions app/controllers/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,30 @@ def create

@company = Company.new(sanitize_params(company_params))

if @company.save
redirect_to @company, notice: t('.success')
saved = @company.save

unless request.xhr?
if @company.save
redirect_to @company, notice: t('.success')
else
flash.now[:alert] = t('.error')
render :new
end
return
end

# XHR request from modal in ShfApplication create view (to create company)
if saved
status = 'success'
id = 'company-number-entry'
html = helpers.company_number_entry_field(@company.company_number)
else
flash.now[:alert] = t('.error')
render :new
status = 'errors'
id = 'company-create-errors'
html = helpers.model_errors_helper(@company)
end

render json: { status: status, id: id, html: html }
end


Expand Down
57 changes: 43 additions & 14 deletions app/controllers/shf_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ShfApplicationsController < ApplicationController

def new
@shf_application = ShfApplication.new(user: current_user)
@company = Company.new
@all_business_categories = BusinessCategory.all
@uploaded_file = @shf_application.uploaded_files.build
end
Expand Down Expand Up @@ -43,13 +44,17 @@ def show


def edit
@all_business_categories = BusinessCategory.all
@company = @shf_application.companies.first
load_update_objects
end


def create
@shf_application = ShfApplication.new(user: current_user)
@shf_application.update(shf_application_params)

@shf_application = ShfApplication.new(shf_application_params
.merge(user: current_user))

set_companies_for_application

if @shf_application.save

Expand Down Expand Up @@ -88,29 +93,37 @@ def update
head :ok
end

elsif @shf_application.update(shf_application_params)
else

if new_file_uploaded params
set_companies_for_application

check_and_mark_if_ready_for_review params['shf_application'] if params.fetch('shf_application', false)
company_valid = @company.valid?

respond_to do |format|
format.js do
head :ok # just let the receiver know everything is OK. no need to render anything
end
if @shf_application.update(shf_application_params) && company_valid

rendering(format, shf_application_params)
if new_file_uploaded params

check_and_mark_if_ready_for_review params['shf_application'] if params.fetch('shf_application', false)

respond_to do |format|
format.js do
head :ok # just let the receiver know everything is OK. no need to render anything
end

rendering(format, shf_application_params)

end

else
update_error(t('.error'))
end

else
@shf_application.errors.add(:companies, :blank) unless company_valid
update_error(t('.error'))
end

else
update_error(t('.error'))
end

end


Expand Down Expand Up @@ -272,11 +285,27 @@ def update_error(error_message)
render json: @shf_application.errors.full_messages, status: :unprocessable_entity if request.xhr?
else
helpers.flash_message(:alert, error_message)
load_update_objects
render :edit
end

end

def load_update_objects
@all_business_categories = BusinessCategory.all
@new_company = Company.new # In case user wants to create a new company
end

def set_companies_for_application
if (company = Company.find_by_company_number(params[:company_number]))
@company = company
@shf_application.companies = [company]
else
@company = Company.new(company_number: params[:company_number])
# @shf_application.companies.clear
end
end


def send_new_app_emails(new_shf_app)

Expand Down
13 changes: 13 additions & 0 deletions app/helpers/companies_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ def pay_branding_fee_link(company_id, user_id)
type: Payment::PAYMENT_TYPE_BRANDING),
{ method: :post, class: 'btn btn-primary btn-xs' })
end

def company_number_selection_field(company_id=nil)
select_tag :company_id,
options_from_collection_for_select(Company.order(:company_number),
:id, :company_number, company_id),
class: 'search_field',
data: {language: "#{@locale}" }
end

def company_number_entry_field(company_number=nil)
number_field_tag :company_number, company_number,
id: 'shf_application_company_number'
end
end
10 changes: 5 additions & 5 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Company < ApplicationRecord

before_save :sanitize_website, :sanitize_description

has_and_belongs_to_many :shf_applications
has_many :company_applications
has_many :shf_applications, through: :company_applications, dependent: :destroy

has_many :users, through: :shf_applications

Expand Down Expand Up @@ -116,17 +117,16 @@ def self.with_members

def destroy_checks

error_if_has_accepted_applications?
error_if_has_applications?

end


# do not delete a Company if it has ShfApplications that are accepted
def error_if_has_accepted_applications?

def error_if_has_applications?
shf_applications.reload

if shf_applications.where(state: 'accepted').any?
if shf_applications.where.not(state: 'being_destroyed').any?
errors.add(:base, 'activerecord.errors.models.company.company_has_active_memberships')
# Rails 5: must throw
throw(:abort)
Expand Down
8 changes: 8 additions & 0 deletions app/models/company_application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CompanyApplication < ApplicationRecord
belongs_to :company
belongs_to :shf_application

validates_presence_of :company, :shf_application

validates_uniqueness_of :company_id, scope: :shf_application
end
8 changes: 6 additions & 2 deletions app/models/concerns/has_swedish_organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ module HasSwedishOrganization

extend ActiveSupport::Concern

KEY = 'activerecord.errors.models.shf_application.attributes.company_number.invalid'

included do

def swedish_organisationsnummer
errors.add(:company_number, "#{self.company_number} is not a valid company number") unless Orgnummer.new(self.company_number).valid?
unless Orgnummer.new(self.company_number).valid?
errors.add(:company_number, I18n.t(KEY, company_number: self.company_number))
end
end

end
end
end
70 changes: 40 additions & 30 deletions app/models/shf_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,39 @@ class ShfApplication < ApplicationRecord
belongs_to :user

# A Company for a membership application (an instantiated one)
# is created (instantiated) only when a membership is *accepted* --
# is created (instantiated) when a embership application is created,
# unless the company already exists, in which case that existing instance
# is associated with a membership application.
# See the 'accept_membership' method below; note the .find_or_create method
#
# Until a membership application is accepted, we just keep the
# company_number. That's what we'll later use to create (instantiate)
# a company if/when needed.
#
has_and_belongs_to_many :companies
# is associated with the new membership application.

has_many :company_applications
has_many :companies, through: :company_applications, dependent: :destroy

has_and_belongs_to_many :business_categories

has_many :uploaded_files

belongs_to :waiting_reason, optional: true,
foreign_key: "member_app_waiting_reasons_id",
class_name: 'AdminOnly::MemberAppWaitingReason'


validates_presence_of :company_number,
:contact_email,
:state
validates_presence_of :contact_email, :state

validates_presence_of :companies

validates_length_of :company_number, is: 10
validates_format_of :contact_email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: [:create, :update]
validates_uniqueness_of :user_id, scope: :company_number
validate :swedish_organisationsnummer

validates_uniqueness_of :user_id

accepts_nested_attributes_for :uploaded_files, allow_destroy: true

accepts_nested_attributes_for :user, update_only: true
# ^^ We are not explicitly using any user attributes in the app form. However,
# we are delegating the "membership_number" getter and setter methods to
# User from ShfApplication ("membership_number" used to be an attribute of
# ShfApplication and was moved to User).
# The update to "membership_number" (via delegation) will not work without
# the above statement.

scope :open, -> { where.not(state: [:accepted, :rejected]) }

Expand All @@ -60,6 +63,7 @@ def company
state :ready_for_review
state :accepted
state :rejected
state :being_destroyed


event :start_review do
Expand All @@ -80,11 +84,11 @@ def company
end

event :accept do
transitions from: [:under_review, :rejected], to: :accepted, after: :accept_membership
transitions from: [:under_review, :rejected], to: :accepted, after: :accept_application
end

event :reject do
transitions from: [:under_review, :accepted], to: :rejected, after: :reject_membership
transitions from: [:under_review, :accepted], to: :rejected, after: :reject_application
end

end
Expand All @@ -100,25 +104,16 @@ def marked_ready_for_review=(value)
end


def swedish_organisationsnummer
errors.add(:company_number, :invalid, company_number: self.company_number) unless errors.include?(:company_number) || Orgnummer.new(self.company_number).valid?
end


def not_a_member?
!user.member?
end


def accept_membership
def accept_application
begin

company = Company.find_or_create_by!(company_number: company_number) do |co|
co.email = contact_email
end

self.companies << company
self.save
# Default company email = user's membership contact email
companies.first.email = contact_email

# email the applicant to let them know the application was approved:
ShfApplicationMailer.app_approved(self).deliver_now
Expand All @@ -130,16 +125,21 @@ def accept_membership
end


def reject_membership
def reject_application

user.update(membership_number: nil)

destroy_uploaded_files

end


def before_destroy_checks

destroy_uploaded_files

destroy_associated_companies

end


Expand All @@ -160,5 +160,15 @@ def destroy_uploaded_files
save
end

def destroy_associated_companies
# Destroy company if no other associated applications

self.update(state: :being_destroyed)

companies.all.each do |cmpy|
cmpy.destroy if cmpy.shf_applications.count == 1
end
end


end
Loading

0 comments on commit f49d7ae

Please sign in to comment.