Module: Redmine::Acts::Customizable::InstanceMethods

Defined in:
lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



47
48
49
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 47

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#available_custom_fieldsObject



51
52
53
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 51

def available_custom_fields
  CustomField.where("type = '#{self.class.name}CustomField'").sorted.to_a
end

#custom_field_value(c) ⇒ Object



122
123
124
125
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 122

def custom_field_value(c)
  field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
end

#custom_field_valuesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 82

def custom_field_values
  @custom_field_values ||= available_custom_fields.collect do |field|
    x = CustomFieldValue.new
    x.custom_field = field
    x.customized = self
    if field.multiple?
      values = custom_values.select { |v| v.custom_field == field }
      if values.empty?
        values << custom_values.build(:customized => self, :custom_field => field)
      end
      x.instance_variable_set("@value", values.map(&:value))
    else
      cv = custom_values.detect { |v| v.custom_field == field }
      cv ||= custom_values.build(:customized => self, :custom_field => field)
      x.instance_variable_set("@value", cv.value)
    end
    x.value_was = x.value.dup if x.value
    x
  end
end

#custom_field_values=(values) ⇒ Object

Sets the values of the object’s custom fields values is a hash like => ‘foo’, 2 => ‘bar’



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 70

def custom_field_values=(values)
  values = values.stringify_keys

  custom_field_values.each do |custom_field_value|
    key = custom_field_value.custom_field_id.to_s
    if values.has_key?(key)
      custom_field_value.value = values[key]
    end
  end
  @custom_field_values_changed = true
end

#custom_field_values_changed?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 107

def custom_field_values_changed?
  @custom_field_values_changed == true
end

#custom_fields=(values) ⇒ Object

Sets the values of the object’s custom fields values is an array like [=> 1, ‘value’ => ‘foo’, => 2, ‘value’ => ‘bar’]



57
58
59
60
61
62
63
64
65
66
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 57

def custom_fields=(values)
  values_to_hash = values.inject({}) do |hash, v|
    v = v.stringify_keys
    if v['id'] && v.has_key?('value')
      hash[v['id']] = v['value']
    end
    hash
  end
  self.custom_field_values = values_to_hash
end

#custom_value_for(c) ⇒ Object



117
118
119
120
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 117

def custom_value_for(c)
  field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  custom_values.detect {|v| v.custom_field_id == field_id }
end

#destroy_custom_value_attachmentsObject



181
182
183
184
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 181

def destroy_custom_value_attachments
  Attachment.where(:container_id => @attachment_custom_value_ids, :container_type => 'CustomValue')
            .destroy_all
end

#reassign_custom_field_valuesObject



156
157
158
159
160
161
162
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 156

def reassign_custom_field_values
  if @custom_field_values
    values = @custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
    @custom_field_values = nil
    self.custom_field_values = values
  end
end

#reload(*args) ⇒ Object



169
170
171
172
173
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 169

def reload(*args)
  @custom_field_values = nil
  @custom_field_values_changed = false
  super
end

#reset_custom_values!Object



164
165
166
167
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 164

def reset_custom_values!
  @custom_field_values = nil
  @custom_field_values_changed = true
end

#save_custom_field_valuesObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 133

def save_custom_field_values
  target_custom_values = []
  custom_field_values.each do |custom_field_value|
    if custom_field_value.value.is_a?(Array)
      custom_field_value.value.each do |v|
        target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
        target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
        target_custom_values << target
      end
    else
      target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
      target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
      target.value = custom_field_value.value
      target_custom_values << target
    end
  end
  self.custom_values = target_custom_values
  custom_values.each(&:save)
  touch if !saved_changes? && custom_values.any?(&:saved_changes?)
  @custom_field_values_changed = false
  true
end

#set_custom_field_default?(custom_value) ⇒ Boolean

Should the default custom field value be set for the given custom_value? By default, default custom field value is set for new objects only

Returns:

  • (Boolean)


113
114
115
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 113

def set_custom_field_default?(custom_value)
  new_record?
end

#store_attachment_custom_value_idsObject



175
176
177
178
179
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 175

def store_attachment_custom_value_ids
  @attachment_custom_value_ids =
    custom_values.select {|cv| cv.custom_field.field_format == 'attachment'}
                 .map(&:id)
end

#validate_custom_field_valuesObject



127
128
129
130
131
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 127

def validate_custom_field_values
  if new_record? || custom_field_values_changed?
    custom_field_values.each(&:validate_value)
  end
end

#visible_custom_field_valuesObject



103
104
105
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 103

def visible_custom_field_values
  custom_field_values.select(&:visible?)
end