Sha256: ccdf76ae71a724258e6ba042b8bad51586b40c309966b41669346b36c0f44a52

Contents?: true

Size: 1.93 KB

Versions: 6

Compression:

Stored size: 1.93 KB

Contents

module Gaku
  class Preset < ActiveRecord::Base

    PRESETS = {
      student: %w(students_gender address_country address_state address_city),
      locale:  %w(language),
      grading: %w(grading_method grading_scheme),
      default: %w(chooser_table_columns),
      pagination: %w(default_per_page students_per_page changes_per_page),
      output_formats: %w(spreadsheets printables documents),
      names: %w(names)
    }

    def self.save_presets(params)
      ActiveRecord::Base.transaction do
        params.each do |preset|
          send(:set, preset[0], preset[1])
        end
      end
    end

    def self.method_missing(method, *args, &block)
      return send method, *args, &block if self.respond_to? method

      method_name = method.to_s
      if method_name =~ /=/
        return set method_name.gsub('=', ''), args.first

      elsif method_name =~ /^.*_per_page$/
        if get(method_name)
          return get method_name
        else
          return get 'default_per_page' # default_per_page
        end

      else
        return get method_name
      end
    end

    private

    def self.set(key, value)
      preset = Preset.where(name: key).first_or_initialize
      preset.update_attribute(:content, value.to_yaml)
    end

    def self.get(key)
      preset =  Preset.where(name: key).first
      preset.nil? ? nil : YAML.load(preset.content)
    end

    def self.load_or_create(presets)
      @presets = Preset.where(name: presets)
      if presets.count == @presets.length
        @presets
      else
        presets.each do |preset|
          Preset.where(name: preset).first_or_create
        end
        @presets = Preset.where(name: presets)
      end
    end

    def self.load_presets_hash(presets)
      presets_hash = {}
      @presets = Preset.where(name: presets)
      @presets.each do |preset|
        presets_hash[preset.name.to_sym] = YAML.load(preset.content) rescue nil
      end
      presets_hash
    end

  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
gaku_core-0.0.3 app/models/gaku/preset.rb
gaku-0.0.3 core/app/models/gaku/preset.rb
gaku-0.0.2 core/app/models/gaku/preset.rb
gaku_core-0.0.2 app/models/gaku/preset.rb
gaku-0.0.1 core/app/models/gaku/preset.rb
gaku_core-0.0.1 app/models/gaku/preset.rb