Scholle
Scholle

Reputation: 1521

Detect whether an acts_as_something line is present in a model or not?

Assuming the following module which allows to add acts_as_timeable functionality to an arbitrary model.

module Timeable
  module ActsAsTimeable
    extend ActiveSupport::Concern

    module ClassMethods
      def acts_as_timeable(options ={})
        ...
      end
    end
  end
end

ActiveRecord::Base.send :include, Timeable::ActsAsTimeable

According to the last line, acts_as_timeable class method is made available in ActiveRecord::Base. So any model extending form ActiveRecord::Base will return true when calling Model.respond_to?(:acts_as_timeable) => true.

How can I detect whether a model actually acts_as_timeable based on whether a line starting with acts_as_timeable...

class Model < ActiveRecord::Base
  acts_as_timeable
end

...(and maybe some options) has been added to the model or not?

Upvotes: 0

Views: 78

Answers (1)

f.svehla
f.svehla

Reputation: 71

I think that the simplest way to do this is to set this state on the model class itself.

In acts_as_timetable you can set a class variable, and expose it through an accessor, like this:

  module ClassMethods
    def timeable?
      !!@timeable
    end

    def acts_as_timeable(options = {})
      @timeable = true

      # Rest of your code
    end
  end

Then you can check simply with MyModel.timeable?

Upvotes: 2

Related Questions