Class: Campaign

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

closed       :boolean          default(FALSE), not null
closed_at    :datetime
created_at   :datetime         not null
creator_id   :integer
description  :text
harvest_year :integer
id           :integer          not null, primary key
lock_version :integer          default(0), not null
name         :string           not null
updated_at   :datetime         not null
updater_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

.first_of_allObject


73
74
75
# File 'app/models/campaign.rb', line 73

def first_of_all
  Campaign.reorder(:harvest_year, :id).first
end

.of(year) ⇒ Object


68
69
70
71
# File 'app/models/campaign.rb', line 68

def of(year)
  raise 'Invalid year: ' + year.inspect unless year.to_s =~ /\A\d+\z/
  find_or_create_by!(harvest_year: year)
end

Instance Method Details

#activitiesObject


82
83
84
# File 'app/models/campaign.rb', line 82

def activities
  Activity.of_campaign(self)
end

#activity_productionsObject


78
79
80
# File 'app/models/campaign.rb', line 78

def activity_productions
  ActivityProduction.of_campaign(self)
end

#closeObject


126
127
128
129
# File 'app/models/campaign.rb', line 126

def close
  update_column(closed: true)
  Ekylibre::Hook.publish(:campaign_closing, campaign_id: id)
end

#followingObject

Return the following campaign


118
119
120
# File 'app/models/campaign.rb', line 118

def following
  self.class.where('harvest_year > ?', harvest_year).order(:harvest_year).first
end

#interventionsObject


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

def interventions
  Intervention.of_campaign(self)
end

#net_surface_areaObject Also known as: shape_area

FIXME: Not generic. Some activities have no shape


103
104
105
# File 'app/models/campaign.rb', line 103

def net_surface_area
  activity_productions.map(&:support_shape_area).sum
end

#opened?Boolean

Returns:

  • (Boolean)

122
123
124
# File 'app/models/campaign.rb', line 122

def opened?
  !closed
end

#precedingObject

Return the previous campaign


113
114
115
# File 'app/models/campaign.rb', line 113

def preceding
  self.class.where('harvest_year < ?', harvest_year).order(harvest_year: :desc).first
end

#previousObject


108
109
110
# File 'app/models/campaign.rb', line 108

def previous
  self.class.where('harvest_year < ?', harvest_year)
end

#productionsObject

Returns all CampaignProduction. These productions always last the campaign duration


92
93
94
# File 'app/models/campaign.rb', line 92

def productions
  CampaignProduction.of(self)
end

#productions_cost_amountObject

Returns the cost of production for the given campaign. It's the sum of all “campaign production” production cost


98
99
100
# File 'app/models/campaign.rb', line 98

def productions_cost_amount
  productions.map(:cost_amount).sum
end

#reopenObject


131
132
133
134
# File 'app/models/campaign.rb', line 131

def reopen
  update_column(closed: false)
  Ekylibre::Hook.publish(:campaign_reopening, campaign_id: id)
end