Class: Crumb

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/crumb.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: crumbs

accuracy                  :decimal(19, 4)   not null
created_at                :datetime         not null
creator_id                :integer
device_uid                :string           not null
geolocation               :geometry({:srid=>4326, :type=>"point"}) not null
id                        :integer          not null, primary key
intervention_parameter_id :integer
lock_version              :integer          default(0), not null
metadata                  :text
nature                    :string           not null
read_at                   :datetime         not null
updated_at                :datetime         not null
updater_id                :integer
user_id                   :integer

Class Method Summary collapse

Instance Method Summary collapse

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

.interventions_paths(user) ⇒ Object

Returns all crumbs, grouped by interventions paths, for a given user. The result is an array of interventions paths. An intervention path is an array of crumbs, for a user, ordered by read_at, between a start crumb and a stop crumb. if data is inconsistent (e.g. no “stop” crumb corresponding to a “start” crumb) the buffer stores crumbs until the next “start” crumb in the chronological order, and the result receives what is found, whatever the crumbs table content, since the user may always requalify crumbs manually. TODO : put this into User model


139
140
141
142
# File 'app/models/crumb.rb', line 139

def interventions_paths(user)
  ActiveSupport::Deprecation.warn('Use User#interventions_paths instead')
  user.interventions_paths
end

.production_supports(*crumbs) ⇒ Object

returns all production supports whose storage shape contains the given crumbs

Parameters

- crumbs: an array of crumbs

Options

- campaigns: one or several campaigns for which production supports are looked for. Default: current campaigns.
  Accepts the same parameters as ActivityProduction.of_campaign since it actually calls this method.

TODO: when refactoring, move this method to ActivityProduction model, as ActivityProduction#of_crumbs(crumbs = [], options = {})


122
123
124
125
126
127
128
# File 'app/models/crumb.rb', line 122

def production_supports(*crumbs)
  options = crumbs.extract_options!
  options[:campaigns] ||= Campaign.current
  ActivityProduction.of_campaign(options[:campaigns].first).distinct
                    .joins(:support)
                    .where('products.id IN (?)', Crumb.products(*crumbs, no_contents: true).map(&:id))
end

.products(*crumbs) ⇒ Object

Returns all products whose shape contains the given crumbs or any crumb if no crumb is given options: no_content: excludes contents. Default: false TODO: when refactoring, move this method to Product model, as Product#of_crumbs(*crumbs)


104
105
106
107
108
109
110
111
112
113
# File 'app/models/crumb.rb', line 104

def products(*crumbs)
  options = crumbs.extract_options!
  crumbs.flatten!
  raw_products = Product.distinct.joins(:readings)
                        .joins("INNER JOIN crumbs ON (indicator_datatype = 'geometry' AND ST_Contains(ST_CollectionExtract(product_readings.geometry_value, 3), crumbs.geolocation))")
                        .where(crumbs.any? ? ['crumbs.id IN (?)', crumbs.map(&:id)] : 'crumbs.id IS NOT NULL')
  contents = []
  contents = raw_products.map(&:contents) unless options[:no_contents]
  raw_products.concat(contents).flatten.uniq
end

Instance Method Details

#followingObject

Returns the next crumb if it exists


91
92
93
# File 'app/models/crumb.rb', line 91

def following
  siblings.after(read_at).reorder(read_at: :asc).first
end

#intervention_pathObject

returns all the crumbs corresponding to the same intervention as the current crumb, i.e. the nearest start crumb including itself, the nearest stop crumb including itself, and all the crumbs in between including the crumb itself.


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/crumb.rb', line 147

def intervention_path
  start_read_at = read_at.utc
  unless start?
    start = siblings.where(nature: :start)
                    .where('read_at <= ?', start_read_at)
                    .order(read_at: :desc)
                    .first
    start_read_at = start.read_at.utc if start
  end
  stop_read_at = read_at.utc
  unless stop?
    stop = siblings.where(nature: :stop)
                   .where('read_at >= ?', stop_read_at)
                   .order(nature: :desc, read_at: :asc)
                   .first
    stop_read_at = stop.read_at.utc if stop
  end
  CrumbSet.new(siblings.where(read_at: start_read_at..stop_read_at).order(read_at: :asc))
end

#previousObject

Returns the previous crumb if it exists


86
87
88
# File 'app/models/crumb.rb', line 86

def previous
  siblings.before(read_at).reorder(read_at: :desc).first
end

#siblingsObject

Returns siblings of the crumbs (same user, same device)


96
97
98
# File 'app/models/crumb.rb', line 96

def siblings
  Crumb.where(user_id: user_id, device_uid: device_uid).order(read_at: :asc)
end