Class: CrumbSet

Inherits:
Object
  • Object
show all
Defined in:
app/models/crumb_set.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2009-2012 Brice Texier, Thibaud Merigon Copyright (C) 2012-2015 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.

Constant Summary collapse

DEFAULT_ACCURACY =

Data are metrics and in meter and square meter

3
DEFAULT_ACCURACY_AREA =
(((DEFAULT_ACCURACY * 0.5)**2) * Math::PI).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(crumbs) ⇒ CrumbSet

Returns a new instance of CrumbSet.


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/crumb_set.rb', line 32

def initialize(crumbs)
  @crumbs = crumbs
  @start = @crumbs.first
  if @start
    @start. ||= {}
    @procedure = Procedo.find(@start.['procedure_nature'])
    unless @procedure
      Rails.logger.warn "Unknown procedure nature: #{@start.['procedure_nature'].inspect}"
    end
  else
    Rails.logger.warn 'Unknown procedure nature: No points, so no start point...'
  end
  @started_at = @start.read_at
  @stopped_at = @crumbs.last.read_at
  @user = @start.user
  @device_uid = @start.device_uid
  @intervention_parameter = @start.intervention_parameter
end

Instance Attribute Details

#crumbsObject (readonly)

Returns the value of attribute crumbs


23
24
25
# File 'app/models/crumb_set.rb', line 23

def crumbs
  @crumbs
end

#device_uidObject (readonly)

Returns the value of attribute device_uid


23
24
25
# File 'app/models/crumb_set.rb', line 23

def device_uid
  @device_uid
end

#intervention_parameterObject (readonly)

Returns the value of attribute intervention_parameter


23
24
25
# File 'app/models/crumb_set.rb', line 23

def intervention_parameter
  @intervention_parameter
end

#procedureObject (readonly)

Returns the value of attribute procedure


23
24
25
# File 'app/models/crumb_set.rb', line 23

def procedure
  @procedure
end

#startObject (readonly)

Returns the value of attribute start


23
24
25
# File 'app/models/crumb_set.rb', line 23

def start
  @start
end

#started_atObject (readonly)

Returns the value of attribute started_at


23
24
25
# File 'app/models/crumb_set.rb', line 23

def started_at
  @started_at
end

#stopped_atObject (readonly)

Returns the value of attribute stopped_at


23
24
25
# File 'app/models/crumb_set.rb', line 23

def stopped_at
  @stopped_at
end

#userObject (readonly)

Returns the value of attribute user


23
24
25
# File 'app/models/crumb_set.rb', line 23

def user
  @user
end

Instance Method Details

#casted?Boolean

Returns:

  • (Boolean)

59
60
61
# File 'app/models/crumb_set.rb', line 59

def casted?
  !@intervention_parameter.nil?
end

#convert!(options = {}) ⇒ Object

Turns a crumb set into an actual intervention and returns the created intervention

Options:

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :procedure_name (String)

    The name of the procedure for which the user wants to register an intervention.

  • :issue_id (String)

    The ID of an issue the intervention is related to


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/crumb_set.rb', line 78

def convert!(options = {})
  intervention = nil
  Ekylibre::Record::Base.transaction do
    options[:procedure_name] ||= @procedure.name
    procedure = Procedo.find(options[:procedure_name])

    # preparing attributes for Intervention#create!
    attributes = {}
    attributes[:procedure_name] = procedure.name

    # Working periods
    attributes[:working_periods_attributes] = []
    attributes[:working_periods_attributes] << {
      started_at: started_at,
      stopped_at: stopped_at
    }

    # Parameters
    options[:working_width] ||= 6
    attributes = assign_parameters(procedure, attributes, options.slice(:working_width))

    # Create intervention
    intervention = Intervention.create!(attributes)
  end
  intervention
end

#human_nameObject


51
52
53
# File 'app/models/crumb_set.rb', line 51

def human_name
  :intervention_at.tl(intervention: procedure_name, at: @start.read_at.l)
end

#idObject


55
56
57
# File 'app/models/crumb_set.rb', line 55

def id
  "set_#{start.id}"
end

#interventionObject


67
68
69
# File 'app/models/crumb_set.rb', line 67

def intervention
  (@intervention_parameter ? @intervention_parameter.intervention : nil)
end

#procedure_nameObject


63
64
65
# File 'app/models/crumb_set.rb', line 63

def procedure_name
  (@procedure ? @procedure.human_name : :unknown.tl)
end

#workerObject


105
106
107
108
# File 'app/models/crumb_set.rb', line 105

def worker
  return nil unless user
  @worker ||= user.worker
end

#working_zone(working_width = nil) ⇒ Object


110
111
112
113
114
115
116
117
118
# File 'app/models/crumb_set.rb', line 110

def working_zone(working_width = nil)
  working_width ||= DEFAULT_ACCURACY
  unless @working_zone && @working_width == working_width
    @working_width = working_width
    line = Charta.make_line(crumbs.order(:read_at).map(&:geolocation))
    @working_zone = line.buffer(@working_width)
  end
  @working_zone
end