Sha256: b01bdbad5cd0e9c6bdbe22a18a6a0d9f4bfac211a4979483d03e90f83e814cae

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

class Eventable::Categorylist

  attr_reader :event, :items
  delegate :id, :to => :event

  class CategoryItem
    attr_reader :id, :event, :name, :group
    attr_accessor :active

    def initialize(event, category)
      @event = event
      @name = category.name
      @group = category.group
      @id = category.id
      @active = event.category_ids.include? @id
    end

    def new_record?
      true
    end
  end

  def initialize(event)
    @event = event
    @items = Eventable::Category.all.map { |c| CategoryItem.new(event, c)}
  end

  def errors
    ActiveRecord::Errors.new(self)
  end

  def items_attributes=(attr)
    attr.values.each do |a|
      @items.detect { |i| i.id == a[:id].to_i }.active = (a[:active] == '1')
    end
  end

  def update_attributes(attrs)
    attrs.each do |k,v|
      send "#{k}=", v
    end
    save
  end

  def save
    @event.category_ids = @items.select(&:active).map(&:id)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dcs-eventable-0.0.8 app/models/eventable/categorylist.rb