Sha256: e573460f2a118bca44ed8644f82f23453f3170dfd458d4f2688ff540c339e78e

Contents?: true

Size: 834 Bytes

Versions: 3

Compression:

Stored size: 834 Bytes

Contents

require 'toy/proxies/list'

module Toy
  class List
    include Toy::Collection

    def after_initialize
      model.attribute(key, Array)
      create_accessors
    end

    def key
      @key ||= :"#{name.to_s.singularize}_ids"
    end

    private

    def proxy_class
      Toy::Proxies::List
    end

    def list_method
      :lists
    end

    def create_accessors
      model.class_eval """
        def #{name}
          #{instance_variable} ||= self.class.#{list_method}[:#{name}].new_proxy(self)
        end

        def #{name}=(records)
          #{name}.replace(records)
        end
      """

      if options[:dependent]
        model.class_eval """
          after_destroy :destroy_#{name}
          def destroy_#{name}
            #{name}.each { |o| o.destroy }
          end
        """
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
toystore-0.10.1 lib/toy/list.rb
toystore-0.10.0 lib/toy/list.rb
toystore-0.9.0 lib/toy/list.rb