aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorZog2018-04-24 09:04:13 +0200
committerJohan Van Ryseghem2018-04-25 15:46:33 +0200
commitc3e6817f2cb2e35c90434574efac256d62636830 (patch)
tree9f73835363786994539c637123366d60013d58d7 /app
parentf838c165685972992d1ee7f597c9d94662099d89 (diff)
downloadchouette-core-c3e6817f2cb2e35c90434574efac256d62636830.tar.bz2
Refs #6669; Fix CustomField initialization
Use the current referential to infer the workgroup
Diffstat (limited to 'app')
-rw-r--r--app/models/application_model.rb16
-rw-r--r--app/models/concerns/custom_fields_support.rb16
-rw-r--r--app/models/custom_field.rb2
3 files changed, 26 insertions, 8 deletions
diff --git a/app/models/application_model.rb b/app/models/application_model.rb
index 1a2a5099d..1715e06c4 100644
--- a/app/models/application_model.rb
+++ b/app/models/application_model.rb
@@ -2,4 +2,20 @@ class ApplicationModel < ::ActiveRecord::Base
include MetadataSupport
self.abstract_class = true
+
+ def self.referential
+ Referential.where(slug: Apartment::Tenant.current).last
+ end
+
+ def referential
+ self.class.referential
+ end
+
+ def self.workgroup
+ referential&.workgroup
+ end
+
+ def workgroup
+ self.class.workgroup
+ end
end
diff --git a/app/models/concerns/custom_fields_support.rb b/app/models/concerns/custom_fields_support.rb
index c39dfd1fc..8d4b37657 100644
--- a/app/models/concerns/custom_fields_support.rb
+++ b/app/models/concerns/custom_fields_support.rb
@@ -5,13 +5,15 @@ module CustomFieldsSupport
validate :custom_fields_values_are_valid
after_initialize :initialize_custom_fields
- def self.custom_fields workgroup=:all
+ def self.custom_fields _workgroup=nil
+ _workgroup ||= self.workgroup
+ return [] unless _workgroup
fields = CustomField.where(resource_type: self.name.split("::").last)
- fields = fields.where(workgroup_id: workgroup&.id) if workgroup != :all
+ fields = fields.where(workgroup_id: _workgroup.id)
fields
end
- def self.custom_fields_definitions workgroup=:all
+ def self.custom_fields_definitions workgroup=nil
Hash[*custom_fields(workgroup).map{|cf| [cf.code, cf]}.flatten]
end
@@ -24,7 +26,7 @@ module CustomFieldsSupport
end
end
- def custom_fields workgroup=:all
+ def custom_fields workgroup=nil
CustomField::Collection.new self, workgroup
end
@@ -43,8 +45,8 @@ module CustomFieldsSupport
def initialize_custom_fields
return unless self.attributes.has_key?("custom_field_values")
self.custom_field_values ||= {}
- custom_fields(:all).values.each &:initialize_custom_field
- custom_fields(:all).each do |k, v|
+ custom_fields.values.each &:initialize_custom_field
+ custom_fields.each do |k, v|
custom_field_values[k] ||= v.default_value
end
@custom_fields_initialized = true
@@ -56,7 +58,7 @@ module CustomFieldsSupport
private
def custom_fields_values_are_valid
- custom_fields(:all).values.all?{|cf| cf.valid?}
+ custom_fields.values.all?{|cf| cf.valid?}
end
end
end
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 88783b5b4..b5c08465d 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -8,7 +8,7 @@ class CustomField < ApplicationModel
validates :code, uniqueness: {scope: [:resource_type, :workgroup_id], case_sensitive: false}, presence: true
class Collection < HashWithIndifferentAccess
- def initialize object, workgroup=:all
+ def initialize object, workgroup=nil
vals = object.class.custom_fields(workgroup).map do |v|
[v.code, CustomField::Instance.new(object, v, object.custom_field_value(v.code))]
end