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

Enhancing observers clean #2630

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
3 changes: 2 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def initialize(user)
def can?(action, subject, *extra_args)
return true if action == :digest
if action == :json_edit
subject.is_a?(Mongoff::Record) && !subject.is_a?(Mongoff::GridFs::File)
(subject.is_a?(Mongoff::Record) && !subject.is_a?(Mongoff::GridFs::File)) ||
subject.is_a?(Setup::Observer)
elsif (action == :simple_cross && crossing_models.exclude?(subject.is_a?(Class) ? subject : subject.class)) ||
(subject == ScriptExecution && (user.nil? || !user.super_admin?))
false
Expand Down
62 changes: 61 additions & 1 deletion app/models/setup/observer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Setup
class Observer < Event
include TriggersFormatter
include ObserverConditions
include RailsAdmin::Models::Setup::ObserverAdmin
# = Observer
#
Expand All @@ -17,6 +18,10 @@ class Observer < Event

before_save :format_triggers, :check_name

def check_before_save
legacy_triggers_to_conditions && validate_conditions && super
end

def verify_triggers
if changed_attributes.key?('triggers')
self.trigger_evaluator = nil unless changed_attributes.key?('trigger_evaluator_id') && trigger_evaluator
Expand All @@ -38,8 +43,63 @@ def to_s
name ? name : super
end

def legacy_operator_to_conditions (operator, value)
conditions = []
case operator
when "starts_with"
conditions.push({'$regex' => '\A'.concat(value)})
when "ends_with"
conditions.push({'$regex' => value.concat('\Z')})
when "like"
conditions.push({'$regex' => value})
when "between"
conditions.push({'$gte' => value [1]})
conditions.push({'$lte' => value [2]})
when "is", "default"
conditions.push({'$eq' => value})
when nil # consider use else
case value
when "_not_null"
conditions.push({'$ne' => nil})
when "_null"
conditions.push({'$eq' => nil})
when "_change"
conditions.push({'$change' => true})
when "_presence_change"
conditions.push({'$ne' => nil})
conditions.push({'$change' => true})
when Array
conditions.push({'$in' => value})
else
conditions.push({'$eq' => value})
end
else
errors.add(:triggers, "have an invalid operator #{operator}.")
end
conditions
end

def legacy_triggers_to_conditions(triggers = self.triggers)
if conditions.blank?
conditions = {}
triggers = JSON.parse(triggers) if triggers.is_a? (String)
triggers.each do |field, value|
value.each do |cond|
conditions[field] = [] unless conditions[field]
new_conds = legacy_operator_to_conditions(cond['o'], cond['v'])
break unless new_conds.size > 0
conditions[field] += new_conds
end
end
self.conditions = conditions if errors.blank?
end
errors.blank?
end

def triggers_apply_to?(obj_now, obj_before = nil)
if triggers
if conditions.present?
conditions_apply_to?(obj_now, obj_before)
elsif triggers
field_triggers_apply_to?(:triggers, obj_now, obj_before)
elsif trigger_evaluator.parameters.count == 1
trigger_evaluator.run(obj_now).present?
Expand Down
Loading