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

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

Overview

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#available_custom_fieldsObject



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

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

#custom_field_value(c) ⇒ Object



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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 87

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.value = values.map(&:value)
    else
      cv = custom_values.detect { |v| v.custom_field == field }
      cv ||= custom_values.build(:customized => self, :custom_field => field)
      x.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'



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 66

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)
      value = values[key]
      if value.is_a?(Array)
        value = value.reject(&:blank?).map(&:to_s).uniq
        if value.empty?
          value << ''
        end
      else
        value = value.to_s
      end
      custom_field_value.value = value
    end
  end
  @custom_field_values_changed = true
end

#custom_field_values_changed?Boolean

Returns:

  • (Boolean)


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

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']



53
54
55
56
57
58
59
60
61
62
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 53

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



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

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

#reassign_custom_field_valuesObject

Since:

  • 3.2.2



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

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_with_custom_fields(*args) ⇒ Object

Deprecated.

Removed at 3.4.0

Since:

  • 2.3.0



167
168
169
170
171
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 167

def reload_with_custom_fields(*args)
  @custom_field_values = nil
  @custom_field_values_changed = false
  reload_without_custom_fields(*args)
end

#reset_custom_values!Object



162
163
164
165
# File 'lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb', line 162

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

#save_custom_field_valuesObject



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

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)
  @custom_field_values_changed = false
  true
end

#validate_custom_field_valuesObject



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

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

#visible_custom_field_valuesObject



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

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