Skip to content

support for namespace and fixed unscoped in model #4

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 2 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
20 changes: 10 additions & 10 deletions lib/approval2/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ module ControllerAdditions


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]))
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,9 +38,6 @@ 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
Expand Down
4 changes: 2 additions & 2 deletions lib/approval2/model_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module ModelAdditions
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, -> { unscoped }, :primary_key => 'approved_id', :foreign_key => 'id', :class_name => self.name
belongs_to :approved_record, -> { unscoped }, :foreign_key => 'approved_id', :primary_key => 'id', :class_name => self.name

validates_uniqueness_of :approved_id, :allow_blank => true
validate :validate_unapproved_record
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.1.8"
end