module ActionModel::Concern::ClassMethods

Public Instance Methods

actions() click to toggle source

Return actions context hash for model class.

# File lib/actionmodel/concern.rb, line 47
def actions
  @actions ||= {}
end
acts_as(*args) click to toggle source

Add actions to model.

Add single action to model

class Post
  acts_as :searchable
  ...
end

Add actions to model

class Post
  acts_as :searchable, :viewable
  ...
end

Actions can be added with options. Options applied for each action

class Post
  acts_as :searchable, ignorecase: true
  ...
end
# File lib/actionmodel/concern.rb, line 40
def acts_as(*args)
  options = args.extract_options!

  args.each { |action_name| include_action action_name, options }
end
method_missing(method, *args, &block) click to toggle source

Add action to model.

@example Add historyable action

class Rank
  acts_as_historyable :value, :boost, autofill: true
  ...
end
Calls superclass method
# File lib/actionmodel/concern.rb, line 59
def method_missing(method, *args, &block)
  action = method[/^acts_as_(.+)/, 1]
  if action.nil?
    super method, *args, &block
  else
    include_action *args.unshift(action)
  end
end

Private Instance Methods

action_module(action) click to toggle source

Return action module by action name

# File lib/actionmodel/concern.rb, line 93
def action_module(action)
  "#{self.name.demodulize}::#{action.to_s.camelize}".safe_constantize ||
      "Actions::#{action.to_s.camelize}".safe_constantize
end
find_or_create_action(action_name) click to toggle source

Find or create action by action name

# File lib/actionmodel/concern.rb, line 88
def find_or_create_action(action_name)
  actions[action_name] ||= ActionModel::Context.new
end
include_action(*args) click to toggle source

Include action into model

# File lib/actionmodel/concern.rb, line 71
def include_action(*args)
  return if args.empty?

  action_name = args.shift.to_sym

  action = action_module action_name
  unless action
    raise 'Action %1$s is not found in [%2$s::%3$s, Actions::%3$s].' %
              [ action_name, self.name.demodulize, action_name.to_s.camelize ]
  end

  find_or_create_action(action_name).update *args

  include action unless include? action
end