Sha256: 6aad6036ef98140c7cb9d5ecf5aa01099ecd08628640638ed79286432cef8ba9

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 Bytes

Contents

module Cavy
  class ItemGroup < ::ActiveRecord::Base

    attr_accessor :param_string

    has_many :cavy_items

    before_save :set_params
    validates :title, presence: true

    def type
      title.downcase.gsub(' ', '_')
    end

    def items
      Cavy::Item.where(item_group_id: id)
    end

    def items_json
      all_items = {}
      items.each do |item|
        item_json = {}
        item.data.try(:each) { |key, value| item_json[key] = value }
        all_items[item.id] = item_json
      end
      all_items.to_json
    end

    def items_with_key_value(key, value)
      item_group = Cavy::ItemGroup.find(id)
      item_group.items.where("data.#{key}" => value)
    end

    private

    def set_params
      unless param_string == '' or param_string == nil
        self.params = param_string.gsub(/[,][ ]*/, ',').split(',')
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cavy-0.1.0.beta2 app/models/cavy/item_group.rb