Sha256: ad6c7e9834a867c28f43e3e6935fa8cb18edc6caf7a5ced86b826f62644ef0fc

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

module LazyCrud
  module ClassMethods
    include Constants

    # all REST actions will take place on an instance of this class
    def set_resource(klass)
      self.resource_class = klass
    end

    # for scoping the resource
    # useful for nested routes, such as
    # /event/:event_id/package/:package_id
    # where Event would be the parent class and would scope
    # the package
    def set_resource_parent(klass)
      self.parent_class = klass
    end

    # determine default resource / pareent resource (if applicable)
    # based on the naming convention
    # e.g.: Parent::ModelController
    def set_default_resources
      name = self.name
      namespaced_names = name.split(/::|Controller/)

      model_name = namespaced_names.pop.try(:singularize)
      parent_name = namespaced_names.join('::').try(:singularize)

      if model_name.present?
        set_resource model_name.constantize
      else
        raise "#{model_name} based on #{name} does not exist."
      end

      if parent_name.present?
        parent_klass = parent_name.safe_constantize
        if parent_klass
          set_resource_parent parent_klass
        else
          logger.debug "[lazy_crud] #{parent_name} could not be found as a class / module."
        end
      end
    end

    # the list of parameters to allow through the strong parameter filter
    def set_param_whitelist(*param_list)
      self.param_whitelist = param_list
    end

    ACTIONS_WITH_HOOKS.each do |action|
      # adds a lambda to the hook array
      define_method("before_#{action}") do |func|
        hook_list = self.send("before_#{action}_hooks")
        hook_list ||= []
        hook_list << func

        self.send("before_#{action}_hooks=", hook_list)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lazy_crud-0.9.8.4 lib/lazy_crud/class_methods.rb
lazy_crud-0.9.8.3 lib/lazy_crud/class_methods.rb
lazy_crud-0.9.8.2 lib/lazy_crud/class_methods.rb
lazy_crud-0.9.8.1 lib/lazy_crud/class_methods.rb