Module: AmeeCarbonStore::ClassMethods

Defined in:
lib/amee_rails_layer/amee_carbon_store.rb

Instance Method Summary collapse

Instance Method Details

#has_carbon_data_stored_in_amee(options = {}) ⇒ Object

Class method that configures a class for storing carbon data in amee. Options are as follows:

  • profile - if set will use this model (through a belongs_to relationship) to access the amee profile to store the data under rather than the model itself. Pass the model to use as symbol

    • eg :user or :project. Introduces the need for the profile_id field as described in header

    and the referenced model must store the amee profile key under the field amee_profile (as would be provided by the has_amee_profile decleration)

  • nameless - if set then automatically assign the name field so the user doesn’t have to (still requires the name field in the database as a name must be set to store in amee)

  • has_date_range - will check for the presence of a start_date and end_date on the object and pass that through to AMEE to store with the data. Will also check the name is unique given the dates unless used in conjunction with :nameless option (the two together are therefore not recommended as it will be easy to create overlapping data). Requires the start_date and end_date database fields as described in header.

  • repetitions - allows repetitions of the data at a database level where AMEE doesn’t support it natively. For example multiple journeys can be setup with this option. The value stored in AMEE will be the total for all journeys. Requires the repetitions database field as described in header.

  • singular_types - if using a structure where multiple types are available for a model and the type is stored in the field “#model_type”, this option enforces that only one instance of each type may exist in the database (for a given project if using a project structure)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/amee_rails_layer/amee_carbon_store.rb', line 50

def has_carbon_data_stored_in_amee(options = {})
  
  if options[:profile]
    belongs_to options[:profile]
  else
    has_amee_profile
  end
  
  validates_numericality_of :amount
  validate_on_create :units_are_valid
  validates_presence_of :start_date, :end_date if options[:has_date_range]
  unless options[:nameless]
    if options[:has_date_range]
      validate :name_is_unique_given_date_range
    else
      uniqueness_options = options[:profile] ? {:scope => "#{options[:profile]}_id".to_sym} : {}
      validates_uniqueness_of :name, uniqueness_options
    end
    validates_format_of :name, :with => /\A[\w -]+\Z/, 
      :message => "must be letters, numbers, spaces or underscores only"
    validates_length_of :name, :maximum => 250
  end
  if options[:singular_types]
    validate_on_create :maximum_one_instance_for_each_type
  end
  if options[:repetitions]
    validates_numericality_of :repetitions, :only_integer => true
  end
  
  before_create :add_to_amee
  before_update :update_amee
  after_destroy :delete_from_amee
  
  write_inheritable_attribute(:amee_profile_class, options[:profile]) if options[:profile]
  write_inheritable_attribute(:repetitions, true) if options[:repetitions]
  write_inheritable_attribute(:nameless_entries, true) if options[:nameless]
  write_inheritable_attribute(:has_date_range, true) if options[:has_date_range]
  
  include AmeeCarbonStore::InstanceMethods
end

#update_carbon_cachesObject

This method updates all the carbon caches for instances of this model. Be aware this may take some time depending on the number of rows.



93
94
95
96
97
# File 'lib/amee_rails_layer/amee_carbon_store.rb', line 93

def update_carbon_caches
  find(:all).each do |item|
    item.update_carbon_output_cache
  end.size
end