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-2019 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?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.first_of_allObject


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

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

.of(year) ⇒ Object


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

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

#closeObject


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

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

#followingObject

Return the following campaign


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

def following
  followings.first
end

#followingsObject


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

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

#net_surface_areaObject Also known as: shape_area

FIXME: Not generic. Some activities have no shape


95
96
97
# File 'app/models/campaign.rb', line 95

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

#opened?Boolean

Returns:

  • (Boolean)

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

def opened?
  !closed
end

#precedingObject

Return the previous campaign


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

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

#previousObject


100
101
102
# File 'app/models/campaign.rb', line 100

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

#productionsObject

Returns all CampaignProduction. These productions always last the campaign duration


84
85
86
# File 'app/models/campaign.rb', line 84

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


90
91
92
# File 'app/models/campaign.rb', line 90

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

#reopenObject


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

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