module Resourcify::Resourcify::ClassMethods

Public Instance Methods

resourcified?() click to toggle source

Class method to tag classes using this plugin

# File lib/resourcify/resourcify.rb, line 21
def resourcified?() true end
resourcify(options = {}) click to toggle source
# File lib/resourcify/resourcify.rb, line 19
def resourcify(options = {})
  # Class method to tag classes using this plugin
  def resourcified?() true end
  
  if self.ancestors.include?(ActiveRecord::Base)        # models
    # Add tpl methods
    send :extend,  Model::Tpl

    # Add policy_class method for pundit
    send :extend,  Model::PolicyClass

    # Include filter_by
    send :extend,  Model::FilterBy

    # Include instance methods
    send :include, ModelInstanceMethods

    # Set options
    cattr_accessor :resourcify_options
    self.resourcify_options = options

    options.each do |key, val|
      cattr_accessor "resourcify_#{key.to_s}".to_sym
      self.send("resourcify_#{key.to_s}=", val)

      cattr_accessor "rfy_#{key.to_s}".to_sym
      self.send("rfy_#{key.to_s}=", val)
    end

  elsif self.ancestors.include?(ActionController::Base) # controllers
    # Turn off layout
    layout false

    # Respond to only json requests
    respond_to :json
    
    # Set error with set_error method located in base.rb
    before_action :set_error

    # Set record with set_record method located in base.rb
    before_action :set_record, only: [:show, :update, :destroy]
    
    # Set rescue_froms with methods located in base.rb
    rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
    rescue_from Pundit::NotAuthorizedError,   with: :user_not_authorized

    # Include base.rb with before_action filters & rescue_from methods
    send :include, Controller::Base

    # Include RESTful actions
    if !options[:actions] || options[:actions].include?(:index)
      send :include, Controller::Actions::Index
    end

    if !options[:actions] || options[:actions].include?(:create)
      send :include, Controller::Actions::Create
    end

    if !options[:actions] || options[:actions].include?(:show)
      send :include, Controller::Actions::Show
    end

    if !options[:actions] || options[:actions].include?(:update)
      send :include, Controller::Actions::Update
    end

    if !options[:actions] || options[:actions].include?(:destroy)
      send :include, Controller::Actions::Destroy
    end
  end
end