add methods to directly access custom fields

pull/2550/head
Jan Sandbrink 10 years ago
parent 1bf1cf2c1b
commit e3537be022
  1. 28
      lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb

@ -51,6 +51,7 @@ module Redmine
send :include, Redmine::Acts::Customizable::InstanceMethods send :include, Redmine::Acts::Customizable::InstanceMethods
# Save custom values when saving the customized object # Save custom values when saving the customized object
after_save :save_custom_field_values after_save :save_custom_field_values
after_initialize :add_custom_field_accessors
end end
end end
@ -122,12 +123,37 @@ module Redmine
custom_values.reject { |cv| cv.marked_for_destruction? }.each do |custom_value| custom_values.reject { |cv| cv.marked_for_destruction? }.each do |custom_value|
unless custom_value.valid? unless custom_value.valid?
custom_value.errors.each do |_, message| custom_value.errors.each do |_, message|
errors.add("custom_field_#{custom_value.custom_field.id}".to_sym, message) errors.add(custom_field_accessor_name(custom_value.custom_field).to_sym, message)
end end
end end
end end
end end
def add_custom_field_accessors
available_custom_fields.each do |custom_field|
getter_name = custom_field_accessor_name(custom_field)
setter_name = "#{getter_name}="
define_singleton_method getter_name do
custom_value = custom_value_for(custom_field)
custom_value ? custom_value.typed_value : nil
end
define_singleton_method setter_name do |value|
# N.B. we do no strict type checking here, it would be possible to assign a user
# to an integer custom field...
value = value.id if value.respond_to?(:id)
self.custom_field_values = { custom_field.id => value }
end
end
end
private
def custom_field_accessor_name(custom_field)
"custom_field_#{custom_field.id}"
end
module ClassMethods module ClassMethods
end end
end end

Loading…
Cancel
Save