Sha256: 05fcb823d4093210a0a56d0161750ec9d86ce0fa3a44b49829181c52de5f5f22

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

module Markable
  module ActsAsMarker
    extend ActiveSupport::Concern

    module ClassMethods
      def acts_as_marker(options = {})
        Markable.set_models
        class_eval {
          class << self
            attr_accessor :marker_name
          end
        }
        self.marker_name = self.name.downcase.to_sym

        class_eval do
          has_many :marker_marks, :class_name => 'Markable::Mark', :as => :marker
          include Markable::ActsAsMarker::MarkerInstanceMethods
        end
        Markable.add_marker self
      end
    end

    module MarkerInstanceMethods
      def method_missing( method_sym, *args )
        Markable.models.each { |model_name|
          if method_sym.to_s =~ Regexp.new("^[\\w_]+_#{model_name.downcase.pluralize}$") ||
              method_sym.to_s =~ Regexp.new("^#{model_name.downcase.pluralize}_marked_as(_[\\w_]+)?$")
            model_name.constantize # ping model
            if self.methods.include? method_sym # method has appear
              return self.method(method_sym).call(*args) # call this method
            end
          end
        }
        super
      rescue
        super
      end

      def set_mark mark, markables
        Array.wrap(markables).each do |markable|
          Markable.can_mark_or_raise? self, markable, mark
          markable.mark_as mark, self
        end
      end

      def remove_mark mark, markables
        Markable.can_mark_or_raise? self, markables, mark
        Array.wrap(markables).each do |markable|
          markable.unmark mark, :by => self
        end
      end
    end

  end
end

ActiveRecord::Base.send :include, Markable::ActsAsMarker

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
markable-0.0.8 lib/markable/acts_as_marker.rb
markable-0.0.7 lib/markable/acts_as_marker.rb
markable-0.0.6 lib/markable/acts_as_marker.rb