Sha256: b8e4cd91b670ecf6547fddf64b40a97ad0ce11677cea604584316d03a7b42794

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module Tito
  module Eventable

    def self.included(base)
      base.extend ClassMethods
      base.class_eval do
        property :event
        property :event_id
      end
    end

    def event=(tito_event)
      self.event_id = tito_event.id
      self.attributes[:event] = tito_event
      @event = tito_event
    end

    module ClassMethods

      def event_id_from_params(params)
        return params[:event][:id] if params[:event]
        return params[:filter][:event_id] if params[:filter]
      end

      def account_id_from_params(params)
        return params[:event][:account_id] if params[:event]
        return params[:filter][:account_id] if params[:filter]
      end

      def path(params=nil)
        prefix_path = '%{account_id}/%{event_id}'
        path_params = params.delete(:path) || params
        path_params[:event_id] = event_id_from_params(params)
        path_params[:account_id] = account_id_from_params(params)
        parts = [].unshift(prefix_path % path_params.symbolize_keys)
        parts << table_name

        File.join(*parts)

        rescue KeyError
          raise ArgumentError, "Please make sure to include account_id and event_id"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tito-0.1.11 lib/tito/eventable.rb
tito-0.1.10 lib/tito/eventable.rb
tito-0.1.9 lib/tito/eventable.rb