Skip to content

Commit

Permalink
Improvements #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dombesz committed Aug 9, 2022
1 parent 0d5339f commit 2cbf6f7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/models/custom_actions/conditions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def human_name
end

def fulfilled_by?(work_package, _user)
(work_package.respond_to?(:"#{key}_id") && values.include?(work_package.send(:"#{key}_id"))) ||
values.empty?
values.empty? ||
(work_package.respond_to?(:"#{key}_id") && values.include?(work_package.send(:"#{key}_id")))
end

def key
Expand Down Expand Up @@ -110,7 +110,7 @@ def self.habtm_table
private_class_method :habtm_table

def self.key_id
"#{key}_id".to_sym
@key_id ||= "#{key}_id".to_sym
end
private_class_method :key_id

Expand Down
25 changes: 14 additions & 11 deletions app/services/service_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#++

class ServiceResult
SUCCESS = true
FAILURE = false

attr_accessor :success,
:result,
:errors,
Expand All @@ -37,10 +40,10 @@ class ServiceResult
def self.success(errors: nil,
message: nil,
message_type: nil,
state: ::Shared::ServiceState.new,
dependent_results: [],
state: nil,
dependent_results: nil,
result: nil)
new(success: true,
new(success: SUCCESS,
errors:,
message:,
message_type:,
Expand All @@ -53,10 +56,10 @@ def self.success(errors: nil,
def self.failure(errors: nil,
message: nil,
message_type: nil,
state: ::Shared::ServiceState.new,
dependent_results: [],
state: nil,
dependent_results: nil,
result: nil)
new(success: false,
new(success: FAILURE,
errors:,
message:,
message_type:,
Expand All @@ -69,18 +72,18 @@ def initialize(success: false,
errors: nil,
message: nil,
message_type: nil,
state: ::Shared::ServiceState.new,
dependent_results: [],
state: nil,
dependent_results: nil,
result: nil)
self.success = success
self.result = result
self.state = state
self.state = state if state

initialize_errors(errors)
initialize_errors(errors) unless success
@message = message
@message_type = message_type

self.dependent_results = dependent_results
self.dependent_results = dependent_results if dependent_results
end

alias success? :success
Expand Down
12 changes: 7 additions & 5 deletions lib/api/decorators/link_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ def href=(value)
end

def title
attribute = ::API::Utilities::PropertyNameConverter.to_ar_name(
@property_name,
context: represented
)
@title ||= begin
attribute = ::API::Utilities::PropertyNameConverter.to_ar_name(
@property_name,
context: represented
)

represented.try(attribute).try(@title_attribute)
represented.try(attribute).try(@title_attribute)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def initialize(models,
render_nil: false

def current_user_allowed_to_add_work_packages?
current_user.allowed_to?(:add_work_packages, project, global: project.nil?)
@current_user_allowed_to_add_work_packages ||=
current_user.allowed_to?(:add_work_packages, project, global: project.nil?)
end

def current_user_allowed_to_edit_work_packages?
Expand Down
47 changes: 30 additions & 17 deletions lib/api/v3/work_packages/work_package_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def initialize(model, current_user:, embed_links: false)
end

link :copy,
cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented.project) } do
cache_if: -> { add_work_packages_allowed? } do
next if represented.new_record?

{
Expand All @@ -114,7 +114,7 @@ def initialize(model, current_user:, embed_links: false)
end

link :pdf,
cache_if: -> { current_user_allowed_to(:export_work_packages, context: represented.project) } do
cache_if: -> { export_work_packages_allowed? } do
next if represented.new_record?

{
Expand All @@ -125,7 +125,7 @@ def initialize(model, current_user:, embed_links: false)
end

link :atom,
cache_if: -> { current_user_allowed_to(:export_work_packages, context: represented.project) } do
cache_if: -> { export_work_packages_allowed? } do
next if represented.new_record? || !Setting.feeds_enabled?

{
Expand Down Expand Up @@ -248,7 +248,7 @@ def initialize(model, current_user:, embed_links: false)
end

link :addChild,
cache_if: -> { current_user_allowed_to(:add_work_packages, context: represented.project) } do
cache_if: -> { add_work_packages_allowed? } do
next if represented.milestone? || represented.new_record?

{
Expand Down Expand Up @@ -555,13 +555,35 @@ def to_hash(*args)
super
end

# Permissions
def current_user_watcher?
represented.watchers.any? { |w| w.user_id == current_user.id }
@current_user_watcher ||= represented.watchers.any? { |w| w.user_id == current_user.id }
end

def current_user_update_allowed?
current_user_allowed_to(:edit_work_packages, context: represented.project) ||
current_user_allowed_to(:assign_versions, context: represented.project)
@current_user_update_allowed ||=
current_user_allowed_to(:edit_work_packages, context: represented.project) ||
current_user_allowed_to(:assign_versions, context: represented.project)
end

def view_time_entries_allowed?
@view_time_entries_allowed ||=
current_user_allowed_to(:view_time_entries, context: represented.project) ||
current_user_allowed_to(:view_own_time_entries, context: represented.project)
end

def view_budgets_allowed?
@view_budgets_allowed ||= current_user_allowed_to(:view_budgets, context: represented.project)
end

def export_work_packages_allowed?
@export_work_packages_allowed ||=
current_user_allowed_to(:export_work_packages, context: represented.project)
end

def add_work_packages_allowed?
@add_work_packages_allowed ||=
current_user_allowed_to(:add_work_packages, context: represented.project)
end

def relations
Expand Down Expand Up @@ -604,7 +626,7 @@ def duration=(value)

def ordered_custom_actions
# As the custom actions are sometimes set as an array
represented.custom_actions(current_user).to_a.sort_by(&:position)
@ordered_custom_actions ||= represented.custom_actions(current_user).to_a.sort_by(&:position)
end

# Attachments need to be eager loaded for the description
Expand All @@ -629,15 +651,6 @@ def json_cache_key
Setting.feeds_enabled?]
end

def view_time_entries_allowed?
current_user_allowed_to(:view_time_entries, context: represented.project) ||
current_user_allowed_to(:view_own_time_entries, context: represented.project)
end

def view_budgets_allowed?
current_user_allowed_to(:view_budgets, context: represented.project)
end

def load_complete_model(model)
::API::V3::WorkPackages::WorkPackageEagerLoadingWrapper.wrap_one(model, current_user)
end
Expand Down

0 comments on commit 2cbf6f7

Please sign in to comment.