Class: ProductNature

Inherits:
Ekylibre::Record::Base show all
Includes:
Customizable
Defined in:
app/models/product_nature.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: product_natures

abilities_list            :text
active                    :boolean          default(FALSE), not null
category_id               :integer          not null
created_at                :datetime         not null
creator_id                :integer
custom_fields             :jsonb
derivative_of             :string
derivatives_list          :text
description               :text
evolvable                 :boolean          default(FALSE), not null
frozen_indicators_list    :text
id                        :integer          not null, primary key
linkage_points_list       :text
lock_version              :integer          default(0), not null
name                      :string           not null
number                    :string           not null
picture_content_type      :string
picture_file_name         :string
picture_file_size         :integer
picture_updated_at        :datetime
population_counting       :string           not null
reference_name            :string
subscribing               :boolean          default(FALSE), not null
subscription_days_count   :integer          default(0), not null
subscription_months_count :integer          default(0), not null
subscription_nature_id    :integer
subscription_years_count  :integer          default(0), not null
updated_at                :datetime         not null
updater_id                :integer
variable_indicators_list  :text
variety                   :string           not null

Defined Under Namespace

Classes: Item

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Class Method Details

.flattened_nomenclatureObject

Returns core attributes of nomenclature merge with nature if necessary name, variety, derivative_of, abilities


301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'app/models/product_nature.rb', line 301

def flattened_nomenclature
  @flattened_nomenclature ||= Nomen::ProductNature.list.collect do |item|
    f = (item.frozen_indicators || []).map(&:to_sym)
    v = (item.variable_indicators || []).map(&:to_sym)
    Item.new(
      item.name,
      Nomen::Variety.find(item.variety),
      Nomen::Variety.find(item.derivative_of),
      WorkingSet::AbilityArray.load(item.abilities),
      f + v, f, v
    )
  end
end

.import_all_from_nomenclatureObject

Load.all product nature from product nature nomenclature


352
353
354
355
356
# File 'app/models/product_nature.rb', line 352

def import_all_from_nomenclature
  Nomen::ProductNature.find_each do |product_nature|
    import_from_nomenclature(product_nature)
  end
end

.import_from_nomenclature(reference_name, force = false) ⇒ Object

Load a product nature from product nature nomenclature


324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/product_nature.rb', line 324

def import_from_nomenclature(reference_name, force = false)
  unless item = Nomen::ProductNature.find(reference_name)
    raise ArgumentError, "The product_nature #{reference_name.inspect} is unknown"
  end
  unless category_item = Nomen::ProductNatureCategory.find(item.category)
    raise ArgumentError, "The category of the product_nature #{item.category.inspect} is unknown"
  end
  if !force && nature = ProductNature.find_by_reference_name(reference_name)
    return nature
  end
  attributes = {
    variety: item.variety,
    derivative_of: item.derivative_of.to_s,
    name: item.human_name,
    population_counting: item.population_counting,
    category: ProductNatureCategory.import_from_nomenclature(item.category),
    reference_name: item.name,
    abilities_list: WorkingSet::AbilityArray.load(item.abilities),
    derivatives_list: (item.derivatives ? item.derivatives.sort : nil),
    frozen_indicators_list: (item.frozen_indicators ? item.frozen_indicators.sort : nil),
    variable_indicators_list: (item.variable_indicators ? item.variable_indicators.sort : nil),
    active: true
  }
  attributes[:linkage_points_list] = item.linkage_points if item.linkage_points
  create!(attributes)
end

.items_of_expression(expression) ⇒ Object

Lists ProductNature::Item which match given expression Fully compatible with WSQL


317
318
319
320
321
# File 'app/models/product_nature.rb', line 317

def items_of_expression(expression)
  flattened_nomenclature.select do |item|
    WorkingSet.check_record(expression, item)
  end
end

.matching_model(variety) ⇒ Object

Returns the closest matching model based on the given variety


170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/models/product_nature.rb', line 170

def self.matching_model(variety)
  if item = Nomen::Variety.find(variety)
    for ancestor in item.self_and_parents
      next unless model = begin
                            ancestor.name.camelcase.constantize
                          rescue
                            nil
                          end
      return model if model <= Product
    end
  end
  nil
end

Instance Method Details

#abilitiesObject

Returns list of abilities as an array of ability items from the nomenclature


245
246
247
248
249
# File 'app/models/product_nature.rb', line 245

def abilities
  abilities_list.collect do |i|
    (Nomen::Ability[i.to_s.split(/\(/).first] ? i.to_s : nil)
  end.compact
end

#ability(name) ⇒ Object


251
252
253
254
255
# File 'app/models/product_nature.rb', line 251

def ability(name)
  abilities_list.select do |a|
    a.to_s.split(/\(/).first == name.to_s
  end
end

#able_to?(ability) ⇒ Boolean

Returns:

  • (Boolean)

257
258
259
# File 'app/models/product_nature.rb', line 257

def able_to?(ability)
  of_expression("can #{ability}")
end

#able_to_each?(abilities) ⇒ Boolean

tests if all abilities are present @params: *abilities, a list of abilities to check. Can't be empty @returns: true if all abilities are matched, false if at least one ability is missing

Returns:

  • (Boolean)

264
265
266
# File 'app/models/product_nature.rb', line 264

def able_to_each?(abilities)
  of_expression(abilities.map { |a| "can #{a}" }.join(' and '))
end

#frozen_indicatorsObject

Returns list of froezen indicators as an array of indicator items from the nomenclature


210
211
212
# File 'app/models/product_nature.rb', line 210

def frozen_indicators
  frozen_indicators_list.collect { |i| Nomen::Indicator[i] }.compact
end

#has_indicator?(indicator) ⇒ Boolean

Returns:

  • (Boolean)

160
161
162
# File 'app/models/product_nature.rb', line 160

def has_indicator?(indicator)
  indicators_list.include? indicator
end

#identifiable?Boolean

Returns:

  • (Boolean)

156
157
158
# File 'app/models/product_nature.rb', line 156

def identifiable?
  of_variety?(:animal) || population_counting_unitary?
end

#indicatorsObject

Returns list of all indicators


200
201
202
# File 'app/models/product_nature.rb', line 200

def indicators
  (frozen_indicators + variable_indicators)
end

#indicators_listObject

Returns list of all indicators names


205
206
207
# File 'app/models/product_nature.rb', line 205

def indicators_list
  (frozen_indicators_list + variable_indicators_list)
end

Returns list of indicators as an array of indicator items from the nomenclature


220
221
222
# File 'app/models/product_nature.rb', line 220

def indicators_related_to(aspect)
  indicators.select { |i| i.related_to == aspect }
end

#individual_indicatorsObject

Returns individual indicators


235
236
237
# File 'app/models/product_nature.rb', line 235

def individual_indicators
  indicators_related_to(:individual)
end

#individual_indicators_listObject

Returns individual indicator names


240
241
242
# File 'app/models/product_nature.rb', line 240

def individual_indicators_list
  individual_indicators.map { |i| i.name.to_sym }
end

#linkage_pointsObject

Returns list of abilities as an array of ability items from the nomenclature


269
270
271
# File 'app/models/product_nature.rb', line 269

def linkage_points
  linkage_points_list
end

#matching_modelObject

Returns the matching model for the record


185
186
187
# File 'app/models/product_nature.rb', line 185

def matching_model
  self.class.matching_model(self.variety)
end

#of_expression(expression) ⇒ Object

Permit to check WSQL expression “locally” to ensure performance


165
166
167
# File 'app/models/product_nature.rb', line 165

def of_expression(expression)
  WorkingSet.check_record(expression, self)
end

#picture_path(style = :original) ⇒ Object


273
274
275
# File 'app/models/product_nature.rb', line 273

def picture_path(style = :original)
  picture.path(style)
end

#population_frozen?Boolean

Returns if population is frozen

Returns:

  • (Boolean)

190
191
192
# File 'app/models/product_nature.rb', line 190

def population_frozen?
  population_counting_unitary?
end

#population_moduloObject

Returns the minimum couting element


195
196
197
# File 'app/models/product_nature.rb', line 195

def population_modulo
  (population_counting_decimal? ? 0.0001 : 1)
end

#subscription_durationObject

Return humanized duration


278
279
280
281
282
283
284
# File 'app/models/product_nature.rb', line 278

def subscription_duration
  l = []
  l << 'x_years'.tl(count: self.subscription_years_count) if self.subscription_years_count > 0
  l << 'x_months'.tl(count: self.subscription_months_count) if self.subscription_months_count > 0
  l << 'x_days'.tl(count: self.subscription_days_count) if self.subscription_days_count > 0
  l.to_sentence
end

#subscription_stopped_on(started_on) ⇒ Object

Compute stopped_on date from a started_on date for subsrbing product nature


287
288
289
290
291
292
293
294
# File 'app/models/product_nature.rb', line 287

def subscription_stopped_on(started_on)
  stopped_on = started_on
  stopped_on += self.subscription_years_count.years
  stopped_on += self.subscription_months_count.months
  stopped_on += self.subscription_days_count.months
  stopped_on -= 1.day
  stopped_on
end

#variable_indicatorsObject

Returns list of variable indicators as an array of indicator items from the nomenclature


215
216
217
# File 'app/models/product_nature.rb', line 215

def variable_indicators
  variable_indicators_list.collect { |i| Nomen::Indicator[i] }.compact
end

#whole_indicatorsObject

Returns whole indicators


225
226
227
# File 'app/models/product_nature.rb', line 225

def whole_indicators
  indicators_related_to(:whole)
end

#whole_indicators_listObject

Returns whole indicator names


230
231
232
# File 'app/models/product_nature.rb', line 230

def whole_indicators_list
  whole_indicators.map { |i| i.name.to_sym }
end