Sha256: 175818fd24f0b1748f89f576603f85562f3759261133f0fb3dcda10aaa7010f7

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module ExtremeAas
  class ExtremeActionGroup < ActiveRecord::Base
    include ExtremeActionAbstract

    has_many :action_edges, :class_name => 'ExtremeActionEdge', :foreign_key => 'parent_action_id', :dependent => :destroy
    has_many :actions, :through => :action_edges, :source => :child_action

    validates_uniqueness_of :name

    def self.export path = "#{Rails.root}/config/extreme_aas_groups.yml"
      yaml = YAML::load_file(path)
      if yaml
        processed_groups_ids = []
        yaml.each do |group_name, actions|
          group = ExtremeActionGroup.find_by_name group_name
          if group.nil?
            group = ExtremeActionGroup.create :name => group_name, :volatile => true
          else 
            group.actions = []            
          end
          processed_groups_ids << group.id
          actions.each do |action_yml_params|
            action_params = {}
            action_yml_params.each do |key, value|
              action_params[key.to_sym] = value
            end
            action = ExtremeAction.parse action_params
            if action.nil?
              raise "Action not found. Parameters #{action_params}"
            else
              group.actions << action
            end
          end
        end
        ExtremeActionGroup.destroy_all(['id NOT IN (?) AND volatile = ?', processed_groups_ids, true])
      else
        puts "Warning: couldn't load file in #{path}"
      end
    end

    after_initialize :default_values

    def default_values
      self.volatile ||= false
    end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
extreme_aas-0.0.2 app/models/extreme_aas/extreme_action_group.rb