Skip to content

Commit

Permalink
re-add Analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ionosphere committed Feb 20, 2014
1 parent 5d5697f commit 3e1e3ea
Show file tree
Hide file tree
Showing 29 changed files with 794 additions and 561 deletions.
3 changes: 2 additions & 1 deletion app/controllers/backend/analyses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Backend::AnalysesController < BackendController
list do |t|
t.column :number, url: true
t.column :reference_number, url: true
t.column :nature
t.column :product, url: true
t.column :analyser, url: true
t.column :made_at
Expand All @@ -33,7 +34,7 @@ class Backend::AnalysesController < BackendController
end

list :items, model: :analysis_items, conditions: {analysis_id: 'params[:id]'.c} do |t|
t.column :indicator
t.column :indicator_name
t.column :value
t.column :annotation
end
Expand Down
50 changes: 48 additions & 2 deletions app/models/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# id :integer not null, primary key
# lock_version :integer default(0), not null
# made_at :datetime
# nature :string(255) not null
# number :string(255) not null
# product_id :integer
# reference_number :string(255)
Expand All @@ -39,13 +40,58 @@
#

class Analysis < Ekylibre::Record::Base
enumerize :nature, in: Nomen::AnalysisNatures.all, predicates: true
belongs_to :analyser, class_name: "Entity"
belongs_to :sampler, class_name: "Entity"
belongs_to :product
has_many :items, class_name: "AnalysisItem", foreign_key: :analysis_id, inverse_of: :analysis
#[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
validates_length_of :number, :reference_number, allow_nil: true, maximum: 255
validates_presence_of :number, :sampled_at
validates_length_of :nature, :number, :reference_number, allow_nil: true, maximum: 255
validates_presence_of :nature, :number, :sampled_at
#]VALIDATORS]

acts_as_numbered

scope :between, lambda { |started_at, stopped_at|
where("sampled_at BETWEEN ? AND ?", started_at, stopped_at)
}

# Measure a product for a given indicator
def read!(indicator, value, options = {})
unless indicator.is_a?(Nomen::Item) or indicator = Nomen::Indicators[indicator]
raise ArgumentError, "Unknown indicator #{indicator.inspect}. Expecting one of them: #{Nomen::Indicators.all.sort.to_sentence}."
end
if value.nil?
raise ArgumentError, "Value must be given"
end
options[:indicator_name] = indicator.name
options[:at] = Time.now unless options.has_key?(:at)
unless item = self.items.find_by(indicator_name: indicator.name, read_at: options[:at])
item = self.items.build(indicator_name: indicator.name, read_at: options[:at])
end
item.value = value
item.save!
return item
end

def get(indicator, *args)
unless indicator.is_a?(Nomen::Item) or indicator = Nomen::Indicators[indicator]
raise ArgumentError, "Unknown indicator #{indicator.inspect}. Expecting one of them: #{Nomen::Indicators.all.sort.to_sentence}."
end
options = args.extract_options!
items = self.items.where(indicator_name: indicator.name.to_s)
if items.any?
return items.first.value
end
return nil
end

# Returns value of an indicator if its name correspond to
def method_missing(method_name, *args)
if Nomen::Indicators.all.include?(method_name.to_s)
return get(method_name, *args)
end
return super
end

end
11 changes: 10 additions & 1 deletion app/models/analysis_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
# updater_id :integer
#
class AnalysisItem < Ekylibre::Record::Base
include ReadingStorable
include ReadingStorable, PeriodicCalculable
belongs_to :analysis
#[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
validates_numericality_of :integer_value, allow_nil: true, only_integer: true
Expand All @@ -53,4 +53,13 @@ class AnalysisItem < Ekylibre::Record::Base
validates_inclusion_of :boolean_value, in: [true, false]
validates_presence_of :analysis, :indicator_datatype, :indicator_name
#]VALIDATORS]

delegate :sampled_at, to: :analysis

calculable period: :month, at: :read_at, column: :measure_value_value

scope :between, lambda { |started_at, stopped_at|
joins(:analysis).merge(Analysis.between(started_at, stopped_at))
}

end
4 changes: 2 additions & 2 deletions app/views/backend/cells/last_milk_result_cells/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#
bar_indicators = [:total_bacteria_concentration, :somatic_cell_concentration, :clostridial_spores_concentration].collect{|i| Nomen::Indicators[i] }
for indicator in bar_indicators
items = ProductReading.where(indicator_name: indicator.name).between(@started_at, @stopped_at)
items = AnalysisItem.between(@started_at, @stopped_at).where(indicator_name: indicator.name)
if items.any?
item_h = items.averages_of_periods.sort.inject({}) do |hash, pair|
hash[pair.expr.to_i.to_s] = pair.average.to_s.to_d
Expand All @@ -28,7 +28,7 @@
#
spline_indicators = [:fat_matters_concentration, :protein_matters_concentration, :urea_concentration].collect{|i| Nomen::Indicators[i] }
for indicator in spline_indicators
items = ProductReading.where(indicator_name: indicator.name).between(@started_at, @stopped_at)
items = AnalysisItem.between(@started_at, @stopped_at).where(indicator_name: indicator.name)
if items.any?
item_h = items.averages_of_periods.sort.inject({}) do |hash, pair|
hash[pair.expr.to_i.to_s] = pair.average.to_d
Expand Down
2 changes: 2 additions & 0 deletions config/locales/arb/complement.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ arb:
enumerize:
activity:

analysis:

animal:

animal_group:
Expand Down
16 changes: 15 additions & 1 deletion config/locales/arb/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ arb:
account_balance: "رصيد الحساب"
# activity: "Activité"
# affair: "Affaire"
# analysis: "Analyse"
# analytic_distribution: "Répartition analytique"
# animal: "Animal"
# animal_group: "Groupe d’animaux"
Expand Down Expand Up @@ -1016,6 +1017,8 @@ arb:
amount: "القيمة من دون الضرائب"
amount_with_taxes: "القيمة مع كامل الضرائب" #?
# amounts_scale: "Nombre de chiffres après le virgule"
# analyser: "Analyseur"
# analyses: "Analyses"
# analysis: "Analyses"
annotation: "ملاحظة"
# archived_at: "Archivé le"
Expand Down Expand Up @@ -1165,6 +1168,7 @@ arb:
created_at: "انشأ يوم"
creator: "المنتج"
creator_id: "المنتج" #?
# creator_name: "Nom du créateur"
credit: "الائتمان"
credit_items: "اساليب الائتمانات" #?
# credit_lines: "Lignes d’avoir"
Expand All @@ -1174,6 +1178,7 @@ arb:
# critic_stocks: "Stocks critiques"
crypt_key: "مفتاح التشفير" #?
crypt_mode: "طؤيقة التشفير" #?
# cultivable_zones: "Zones cultivables"
currencies: "العملات" #?
currency: "عملة"
# currency_precision: "Nombre de décimales"
Expand Down Expand Up @@ -1214,6 +1219,7 @@ arb:
default_currency: "العملة الافتراضية" #?
# default_mail_address: "Adresse email par défaut"
# default_storage: "Emplacement par défaut"
# default_variant: "Variant par défault"
deferred_payment: "ائتمان معلق"
delays: "تأخير" #?
deleted_at: "الغيه في"
Expand Down Expand Up @@ -1298,6 +1304,7 @@ arb:
# encrypted_password: "Mot de passe crypté"
# end_junction: "End junction"
# end_way: "End way"
# enjoyer: "Usufruitier"
# enjoyments: "Enjoyments"
entities: "الافراد" #?
entity: "الفرد"
Expand All @@ -1324,7 +1331,7 @@ arb:
establishments: "المؤسسات" #?
# event: "Évènement"
event_natures: "نوع الحدث" #?
events: "الاحداث" #?
events: "الاحداث"
excise: "عدد رسوم الدفع" #?
# exclusive: "Exclusif"
# execution_number: "N° d’exécution"
Expand Down Expand Up @@ -1377,7 +1384,10 @@ arb:
full_name_format: "التصميم" #?
function_title: "الكياسة"
future_events: "احداث المستقبل" #?
# gap: "Ecart"
# gaps: "Ecarts"
generated: "ولدت" #?
# geolocation: "Géolocalisation"
# geometry_value: "Geometry value"
global_balance: "الرصيد العالمي"
global_count: "مجموع السكان"
Expand All @@ -1396,6 +1406,7 @@ arb:
iban_label: "IBAN" #?
id: "ID"
# identification_number: "Numéro d’identification"
# identifiers: "Identifiants"
immobilizations_account_id: "الحساب الساكن" #?
importance: "ذو شأن"
# in_cash: "En espèce"
Expand Down Expand Up @@ -1503,6 +1514,7 @@ arb:
# locked_at: "Verrouillé à"
longitude: "خطوط الطول" #?
lost: "الضائع" #?
# made_at: "Fait le"
mail: "البريد الالكتروني"
# mail_addresses: "Adresses postales"
# mail_auto_update: "Mettre à jour l’adresse automatiquement"
Expand Down Expand Up @@ -1806,6 +1818,8 @@ arb:
sales_invoices: "الفواتير"
sales_invoices_count: "عدد الفواتير" #?
salt: "الملح" #?
# sampled_at: "Prélevé le"
# sampler: "Préleveur"
scheduled: "الدفعة المبرمجة"
second_building: "موقع تخزين الوصول" #?
second_building_id: "موقع تخزين الوصول" #?
Expand Down
13 changes: 11 additions & 2 deletions config/locales/arb/nomenclatures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ arb:
# vegetal_crops: "Production végétale"
# vine_wine: "Vigne et vin"
# name: "Familles d’activité"
analysis_natures:
attributes:
# items: "Items"
# target_derivative_of: "Derivé de la cible"
# target_variety: "Variété de la cible"
choices: #?
items:
# cow_milk_analysis: "Analyse de lait de vache"
# name: "Type d'analyse"
animal_life_states:
items:
# aging: "Adulte"
Expand Down Expand Up @@ -1796,7 +1805,7 @@ arb:
# calf: "Veau"
# cattle_herd: "Troupeau de bovins"
# cereal_crop: "Culture de céréales"
# chemical_fertilizer: "Engrais chimique"
# chemical_fertilizer: "Engrais minéral"
# complete_sower: "Semoir équipé"
# crop: "Culture"
# cultivable_zone: "Zone cultivable"
Expand Down Expand Up @@ -2116,7 +2125,7 @@ arb:
# carmenere_noir: "Carménère noir"
# champagne_soil: "Sols sur craie Sud Charente et Charente-Martime (Terres de champagne)"
# chardonnay: "Chardonnay"
# chemical_fertilizer: "Engrais chimique"
# chemical_fertilizer: "Engrais minéral"
# chesnut_red_soil: "Terres rouges à châtaigniers"
# cichorium: "Chicoré"
# clay_limestone_soil: "Sol argilo-calcaire (Groies)"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/eng/complement.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ eng:
enumerize:
activity:

analysis:

animal:
variety:
animal: "Animal"
Expand Down
16 changes: 15 additions & 1 deletion config/locales/eng/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ eng:
account_balance: "Account balance"
activity: "Activity"
affair: "Affair"
# analysis: "Analyse"
analytic_distribution: "Analytic distribution"
animal: "Animal"
animal_diagnostic: "Animal diagnostic" #?
Expand Down Expand Up @@ -606,6 +607,8 @@ eng:
allocation_account: "Allocation account"
amount: "Amount"
# amounts_scale: "Nombre de chiffres après le virgule"
# analyser: "Analyseur"
# analyses: "Analyses"
# analysis: "Analyses"
animal: "Animal" #?
animal_drugs: "Animal drugs" #?
Expand Down Expand Up @@ -754,6 +757,7 @@ eng:
country: "Country"
created_at: "Created on"
creator: "Creator"
# creator_name: "Nom du créateur"
credit: "Credit"
# credit_lines: "Lignes d’avoir"
credited_quantity: "Credited quantity"
Expand All @@ -762,6 +766,7 @@ eng:
critic_stocks: "Critic stocks"
crypt_key: "Crypt key" #?
crypt_mode: "Crypt mode" #?
# cultivable_zones: "Zones cultivables"
currency: "Currency"
currency_precision: "Currency precision"
currency_rate: "Currency rate"
Expand Down Expand Up @@ -802,6 +807,7 @@ eng:
default_mail_address: "Default mail address"
default_stock: "Default stock" #?
# default_storage: "Emplacement par défaut"
# default_variant: "Variant par défault"
deferred_payment: "Deferred payment"
delays: "Delays" #?
deleted_at: "Deleted at"
Expand Down Expand Up @@ -889,6 +895,7 @@ eng:
encrypted_password: "Encrypted password"
# end_junction: "End junction"
# end_way: "End way"
# enjoyer: "Usufruitier"
# enjoyments: "Enjoyments"
entities: "People" #?
entity: "Entity"
Expand All @@ -910,7 +917,7 @@ eng:
establishments: "Establishments" #?
event: "Event"
event_natures: "Event types" #?
events: "Events" #?
events: "Events"
examinated_at: "Examinated at" #?
excise: "Excise" #?
# exclusive: "Exclusif"
Expand Down Expand Up @@ -962,7 +969,10 @@ eng:
full_name_format: "Full name format" #?
function_title: "Function title"
future_events: "Future events" #?
# gap: "Ecart"
# gaps: "Ecarts"
generated: "Generated" #?
# geolocation: "Géolocalisation"
# geometry_value: "Geometry value"
global_balance: "Global balance"
global_count: "Global count"
Expand All @@ -981,6 +991,7 @@ eng:
iban_label: "IBAN" #?
id: "ID"
identification_number: "Identification number"
# identifiers: "Identifiants"
immobilizations_products: "Immobilizations products" #?
importance: "Importance"
in_cash: "In cash"
Expand Down Expand Up @@ -1082,6 +1093,7 @@ eng:
longitude: "Longitude" #?
lost: "Lost" #?
luminance: "Luminance" #?
# made_at: "Fait le"
mail: "Mail"
# mail_addresses: "Adresses postales"
mail_auto_update: "Mail auto update"
Expand Down Expand Up @@ -1388,6 +1400,8 @@ eng:
sales_invoices: "Sales invoices"
sales_products: "Sales products" #?
salt: "Salt" #?
# sampled_at: "Prélevé le"
# sampler: "Préleveur"
scheduled: "Scheduled"
second_building: "Second building" #?
second_stock_move: "Second stock move" #?
Expand Down
Loading

0 comments on commit 3e1e3ea

Please sign in to comment.