Skip to content

changes for rails v5.1 - iteration 01 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions approval2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 10.0"
spec.add_dependency "audited"
spec.add_dependency "will_paginate"
spec.add_dependency "unscoped_associations"
end
30 changes: 16 additions & 14 deletions lib/approval2/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,31 @@ module ControllerAdditions
extend ActiveSupport::Concern

included do
before_filter :before_edit, only: :edit
before_filter :before_approve, only: :approve
before_filter :before_index, only: :index
before_action :before_edit, only: :edit
before_action :before_approve, only: :approve
before_action :before_index, only: :index
end


private

def modelName
self.class.name.sub("Controller", "").underscore.split('/').last.singularize
end

def modelKlass
moduleName = self.class.name.include?("::") ? self.class.name.split("::").first : ""
"#{moduleName}::#{modelName.classify}".constantize
end

def before_index
if (params[:approval_status].present? and params[:approval_status] == 'U')
modelName = self.class.name.sub("Controller", "").underscore.split('/').last.singularize
modelKlass = modelName.classify.constantize

if (params[:approval_status].present? and params[:approval_status] == 'U')
x = modelKlass.unscoped.where("approval_status =?",'U').order("id desc")
instance_variable_set("@#{modelName}s", x.paginate(:per_page => 10, :page => params[:page]))
instance_variable_set("@#{modelName}s", x)
end
end

def before_edit
modelName = self.class.name.sub("Controller", "").underscore.split('/').last.singularize
modelKlass = modelName.classify.constantize

x = modelKlass.unscoped.find_by_id(params[:id])
if x.approval_status == 'A' && x.unapproved_record.nil?
params = (x.attributes).merge({:approved_id => x.id,:approved_version => x.lock_version})
Expand All @@ -35,17 +38,16 @@ def before_edit
end

def before_approve
modelName = self.class.name.sub("Controller", "").underscore.split('/').last.singularize
modelKlass = modelName.classify.constantize

x = modelKlass.unscoped.find(params[:id])
modelKlass.transaction do
approved_record = x.approve
if approved_record.save
instance_variable_set("@#{modelName}", approved_record)
flash[:alert] = "#{modelName.humanize.titleize} record was approved successfully"
else
msg = approved_record.errors.full_messages
flash[:alert] = msg
instance_variable_set("@#{modelName}", x)
raise ActiveRecord::Rollback
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/approval2/model_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ module ModelAdditions
included do
audited except: [:approval_status, :last_action]

default_scope { where(approval_status: 'A') }

after_initialize { self.approval_status = 'U' if new_record? }

# refers to the unapproved record in the common unapproved_record model, this is true only for U records
has_one :unapproved_record_entry, :as => :approvable, :class_name => '::UnapprovedRecord'

# refers to the approved/unapproved record in the model
belongs_to :unapproved_record, :primary_key => 'approved_id', :foreign_key => 'id', :class_name => self.name, :unscoped => true
belongs_to :approved_record, :foreign_key => 'approved_id', :primary_key => 'id', :class_name => self.name, :unscoped => true
belongs_to :unapproved_record, -> { unscope(where: :approval_status) }, :primary_key => 'approved_id', :foreign_key => 'id', :class_name => self.name
belongs_to :approved_record, -> { unscope(where: :approval_status) }, :foreign_key => 'approved_id', :primary_key => 'id', :class_name => self.name

validates_uniqueness_of :approved_id, :allow_blank => true
validate :validate_unapproved_record

def self.default_scope
where approval_status: 'A'
end


after_create :on_create_create_unapproved_record_entry
after_destroy :on_destory_remove_unapproved_record_entries
Expand Down Expand Up @@ -67,7 +67,7 @@ def enable_approve_button?
self.approval_status == 'U' ? true : false
end

def on_create_create_unapproved_record_entry
def on_create_create_unapproved_record_entry
if approval_status == 'U'
UnapprovedRecord.create!(:approvable => self)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/approval2/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Approval2
VERSION = "0.1.7"
VERSION = "0.2.0"
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateUnapprovedRecords < ActiveRecord::Migration
class CreateUnapprovedRecords < ActiveRecord::Migration[5.1]
def change
create_table :unapproved_records, {:sequence_start_value => '1 cache 20 order increment by 1'} do |t|
t.integer :approvable_id
Expand Down