Skip to content
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

Some smaller refactorings #3370

Merged
merged 5 commits into from
Aug 19, 2015
Merged
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
10 changes: 4 additions & 6 deletions app/controllers/work_packages/auto_completes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def index

respond_to do |format|
format.html do render layout: false end
format.any(:xml, :json) { render request.format.to_sym => wp_hashes_with_string }
format.any(:xml, :json) { render request.format.to_sym => wp_hashes_with_string(@work_packages) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [103/100]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [103/100]

end
end

private

def wp_hashes_with_string
@work_packages.map do |work_package|
def wp_hashes_with_string(work_packages)
work_packages.map do |work_package|
wp_hash = Hash.new
work_package.attributes.each do |key, value| wp_hash[key] = Rack::Utils.escape_html(value) end
wp_hash['to_s'] = Rack::Utils.escape_html(work_package.to_s)
Expand All @@ -73,9 +73,7 @@ def wp_hashes_with_string
def find_project
project_id = (params[:work_package] && params[:work_package][:project_id]) || params[:project_id]
return nil unless project_id
Project.find(project_id)
rescue ActiveRecord::RecordNotFound
nil
Project.find_by_id(project_id)
end

def determine_scope
Expand Down
33 changes: 10 additions & 23 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,11 @@ def self.search_in_project(query, options)
end

def self.register_allowance_evaluator(filter)
self.registered_allowance_evaluators ||= []

registered_allowance_evaluators << filter
end

# replace by class_attribute when on rails 3.x
class_eval do
def self.registered_allowance_evaluators() nil end
def self.registered_allowance_evaluators=(val)
singleton_class.class_eval do
define_method(:registered_allowance_evaluators) do
val
end
end
end
def self.registered_allowance_evaluators
@@registered_allowance_evaluators ||= []
end

register_allowance_evaluator OpenProject::PrincipalAllowanceEvaluator::Default
Expand Down Expand Up @@ -627,8 +617,6 @@ def allowed_to?(action, context, options = {})
end

def allowed_to_in_project?(action, project, options = {})
initialize_allowance_evaluators

# No action allowed on archived projects
return false unless project.active?
# No action allowed on disabled modules
Expand All @@ -637,11 +625,11 @@ def allowed_to_in_project?(action, project, options = {})
return true if admin?

candidates_for_project_allowance(project).any? do |candidate|
denied = @registered_allowance_evaluators.any? do |filter|
denied = allowance_evaluators.any? do |filter|
filter.denied_for_project? candidate, action, project, options
end

!denied && @registered_allowance_evaluators.any? do |filter|
!denied && allowance_evaluators.any? do |filter|
filter.granted_for_project? candidate, action, project, options
end
end
Expand All @@ -653,14 +641,13 @@ def allowed_to_globally?(action, options = {})
# Admin users are always authorized
return true if admin?

initialize_allowance_evaluators
# authorize if user has at least one membership granting this permission
candidates_for_global_allowance.any? do |candidate|
denied = @registered_allowance_evaluators.any? do |evaluator|
denied = allowance_evaluators.any? do |evaluator|
evaluator.denied_for_global? candidate, action, options
end

!denied && @registered_allowance_evaluators.any? do |evaluator|
!denied && allowance_evaluators.any? do |evaluator|
evaluator.granted_for_global? candidate, action, options
end
end
Expand Down Expand Up @@ -798,18 +785,18 @@ def password_meets_requirements

private

def initialize_allowance_evaluators
@registered_allowance_evaluators ||= self.class.registered_allowance_evaluators.map do |evaluator|
def allowance_evaluators
@allowance_evaluators ||= self.class.registered_allowance_evaluators.map do |evaluator|
evaluator.new(self)
end
end

def candidates_for_global_allowance
@registered_allowance_evaluators.map(&:global_granting_candidates).flatten.uniq
allowance_evaluators.map(&:global_granting_candidates).flatten.uniq
end

def candidates_for_project_allowance(project)
@registered_allowance_evaluators.map { |f| f.project_granting_candidates(project) }.flatten.uniq
allowance_evaluators.map { |f| f.project_granting_candidates(project) }.flatten.uniq
end

def former_passwords_include?(password)
Expand Down
8 changes: 4 additions & 4 deletions app/services/create_work_package_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#++

class CreateWorkPackageService
attr_accessor :user, :project, :work_package
attr_reader :user, :project

def initialize(user:, project:, send_notifications: true)
self.user = user
self.project = project
@user = user
@project = project

JournalManager.send_notification = send_notifications
end
Expand All @@ -43,7 +43,7 @@ def create
author: user,
type: project.types.where(is_default: true).first || project.types.first
}
self.work_package = project.add_work_package(hash)
project.add_work_package(hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! I thought, that we stopped remembering the own work package, long ago...

end

def save(work_package)
Expand Down