Class: Enumeration
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Enumeration
- Includes:
- ASModel
- Defined in:
- backend/app/model/enumeration.rb,
frontend/app/models/enumeration.rb
Instance Attribute Summary (collapse)
-
- (Object) enum_name
Returns the value of attribute enum_name.
-
- (Object) enum_value
Returns the value of attribute enum_value.
Class Method Summary (collapse)
-
+ (Object) apply_values(obj, json, opts = {})
Update the allowable values of the current enumeration.
-
+ (Object) broadcast_changes
-
+ (Object) create_from_json(json, opts = {})
-
+ (Object) dependants_of(enum_name)
-
+ (Object) register_enumeration_dependant(definition, model)
Record the fact that ‘model’ uses ‘enum_name’.
-
+ (Object) sequel_to_jsonmodel(objs, opts = {})
Instance Method Summary (collapse)
-
- (Object) dependants
-
- (Enumeration) initialize(hash = {})
constructor
A new instance of Enumeration.
-
- (Object) migrate(old_value, new_value)
Find all database records that refer to the enumeration value identified by ‘source_id’ and repoint them to ‘destination_id’.
-
- (Object) update_from_json(json, opts = {}, apply_nested_records = true)
Methods included from ASModel
all_models, included, update_publish_flag, update_suppressed_flag
Methods included from JSONModel
JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_repository, strict_mode, strict_mode?, with_repository
Constructor Details
- (Enumeration) initialize(hash = {})
Returns a new instance of Enumeration
5 6 7 8 9 10 |
# File 'frontend/app/models/enumeration.rb', line 5 def initialize(hash = {}) if hash @enum_name = hash["enum_name"] @enum_value = hash["enum_value"] end end |
Instance Attribute Details
- (Object) enum_name
Returns the value of attribute enum_name
2 3 4 |
# File 'frontend/app/models/enumeration.rb', line 2 def enum_name @enum_name end |
- (Object) enum_value
Returns the value of attribute enum_value
3 4 5 |
# File 'frontend/app/models/enumeration.rb', line 3 def enum_value @enum_value end |
Class Method Details
+ (Object) apply_values(obj, json, opts = {})
Update the allowable values of the current enumeration.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'backend/app/model/enumeration.rb', line 59 def self.apply_values(obj, json, opts = {}) # don't allow update of an non-editable enumeration # make sure the DB mapping has been converted. obj.reload is_editable = ( obj.editable === 1 or obj.editable == true ) incoming_values = Array(json['values']) existing_values = obj.enumeration_value.map {|val| val[:value]} added_values = incoming_values - existing_values removed_values = existing_values - incoming_values # if it's not editable, we cannot add or remove values, but we can set the # default... if ( !is_editable and added_values.length > 0 ) or ( !is_editable and removed_values.length > 0 ) raise AccessDeniedException.new("Cannot modify a non-editable enumeration: #{obj.name} with #{ json['values'].join(' , ') }. Only allowed values are : #{ obj.enumeration_value.join(' , ')} ") end # Make sure we're not being asked to remove read-only values. if EnumerationValue.filter(:enumeration_id => obj.id, :value => removed_values, :readonly => 1).count > 0 raise AccessDeniedException.new("Can't remove read-only enumeration values") end added_values.each_with_index do |value, i| obj.add_enumeration_value(:value => value, :position => (existing_values.length + i + 1) ) end removed_values.each do |value| DB.attempt { EnumerationValue.filter(:enumeration_id => obj.id, :value => value).delete }.and_if_constraint_fails { raise ConflictException.new("Can't delete a value that's in use: #{value}") } end enum_vals = EnumerationValue.filter( :enumeration_id => obj.id ).order(:position) enum_vals.update(:position => Sequel.lit('position + 9999' )) enum_vals.each_with_index do |ev, i| ev.position = i ev.save end broadcast_changes obj.refresh existing_default = obj.default_value.nil? ? nil : obj.default_value[:value] if opts[:default_value] != existing_default if opts[:default_value] new_default = EnumerationValue[:value => opts[:default_value]] return obj if new_default.nil? #just move along if the default isn't in the values table obj.default_value = new_default[:id] else obj.default_value = nil end obj.save end obj end |
+ (Object) broadcast_changes
171 172 173 |
# File 'backend/app/model/enumeration.rb', line 171 def self.broadcast_changes Notifications.notify("ENUMERATION_CHANGED") end |
+ (Object) create_from_json(json, opts = {})
131 132 133 134 135 136 |
# File 'backend/app/model/enumeration.rb', line 131 def self.create_from_json(json, opts = {}) default_value = json['default_value'] json['default_value'] = nil self.apply_values(super, json, opts.merge({:default_value => default_value})) end |
+ (Object) dependants_of(enum_name)
21 22 23 |
# File 'backend/app/model/enumeration.rb', line 21 def self.dependants_of(enum_name) @enumeration_dependants[enum_name] end |
+ (Object) register_enumeration_dependant(definition, model)
Record the fact that ‘model’ uses ‘enum_name’.
13 14 15 16 17 18 |
# File 'backend/app/model/enumeration.rb', line 13 def self.register_enumeration_dependant(definition, model) Array(definition[:uses_enum]).each do |enum_name| @enumeration_dependants[enum_name] ||= [] @enumeration_dependants[enum_name] << [definition, model] end end |
+ (Object) sequel_to_jsonmodel(objs, opts = {})
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'backend/app/model/enumeration.rb', line 147 def self.sequel_to_jsonmodel(objs, opts = {}) jsons = super jsons.zip(objs).each do |json, obj| # we're keeping the values as just the not suppressed values. # enumeration_values are only needed in situations where we are # editing/updating the lists. json['values'] = obj.enumeration_value.map {|v| v[:value] unless v[:suppressed] == 1 } json['readonly_values'] = obj.enumeration_value.map {|v| v[:value] if ( v[:readonly] != 0 && v[:suppressed] != 1 )}.compact json['enumeration_values'] = EnumerationValue.sequel_to_jsonmodel(obj.enumeration_value) # this tells us where the enum is used. json["relationships"] = obj.dependants.collect { |d| d.first[:property] }.uniq if obj.default_value json['default_value'] = EnumerationValue[:id => obj.default_value][:value] end end jsons end |
Instance Method Details
- (Object) dependants
25 26 27 |
# File 'backend/app/model/enumeration.rb', line 25 def dependants self.class.dependants_of(self.name) || [] end |
- (Object) migrate(old_value, new_value)
Find all database records that refer to the enumeration value identified by ‘source_id’ and repoint them to ‘destination_id’.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'backend/app/model/enumeration.rb', line 31 def migrate(old_value, new_value) old_enum_value = self.enumeration_value.find {|val| val[:value] == old_value} if old_enum_value.readonly != 0 raise AccessDeniedException.new("Can't transfer from a read-only enumeration value") end new_enum_value = self.enumeration_value.find {|val| val[:value] == new_value} if new_enum_value.nil? raise NotFoundException.new("Can't find a value '#{new_value}' in enumeration #{self.id}") end dependants = self.class.dependants_of(self.name) ? self.class.dependants_of(self.name) : [] dependants.each do |definition, model| property_id = "#{definition[:property]}_id".intern model.filter(property_id => old_enum_value.id).update(property_id => new_enum_value.id, :system_mtime => Time.now) end old_enum_value.delete self.reload self.enumeration_value.each_with_index { |ev, i| ev.position = i; ev.save } self.class.broadcast_changes end |
- (Object) update_from_json(json, opts = {}, apply_nested_records = true)
139 140 141 142 143 144 |
# File 'backend/app/model/enumeration.rb', line 139 def update_from_json(json, opts = {}, apply_nested_records = true) default_value = json['default_value'] json['default_value'] = nil self.class.apply_values(super, json, opts.merge({:default_value => default_value})) end |