module BaseResource::Actions

Public Instance Methods

br_create(options = {})
Alias for: create
br_destroy()
Alias for: destroy
br_index(resources = nil)
Alias for: index
br_show()
Alias for: show
br_update(resource = nil)
Alias for: update
create(options = {}) { |hash, form| ... } click to toggle source
# File lib/base_resource/actions.rb, line 30
def create options = {}
  options = {auto_render_success: true, auto_render_error: true, custom_save: false}.merge!(options || {})
  form = form_const.new(resource_klass.new)
  instance_variable_set("@#{resource_name(form.model)}", form.model)
  if form.validate(params)
    if options[:custom_save]
      form.save do |hash|
        yield hash, form
      end if block_given?
    else
      if form.save
        yield true if block_given?
        render json: { msg: :successfully_create }, status: 200  and return if options[:auto_render_success]
      else
        yield false if block_given?
        render json: { msg: form.errors.full_messages.first || form.model.errors.full_messages.first }, status: 422 and return if options[:auto_render_error]
      end
    end
  else
    yield false if block_given? && !options[:custom_save]
    render json: { msg: form.errors.full_messages.first }, status: 422  and return if options[:auto_render_error]
  end
end
Also aliased as: br_create
destroy() { |resource| ... } click to toggle source
# File lib/base_resource/actions.rb, line 71
def destroy
  resource = destroy_resource
  instance_variable_set("@#{resource_name(resource)}", resource)
  if block_given?
    yield resource
  else
    render json: { msg: :successfully_destroy }, status: 200
  end
end
Also aliased as: br_destroy
index(resources = nil) { |resources| ... } click to toggle source
# File lib/base_resource/actions.rb, line 10
def index resources = nil
  resources = find_base_resources resources
  if block_given?
    resources = yield(resources)
  end
  instance_variable_set("@#{resources_name(resources)}", paginate(resources.order(id: :desc)))
end
Also aliased as: br_index
show() { |resource| ... } click to toggle source

or in ruby > 2.2, child can call parent method use below method method(:index).super_method.call

# File lib/base_resource/actions.rb, line 21
def show
  resource = find_base_resource
  instance_variable_set("@#{resource_name(resource)}", resource)
  if block_given?
    yield(resource)
  end
end
Also aliased as: br_show
update(resource = nil) { |resource| ... } click to toggle source
# File lib/base_resource/actions.rb, line 55
def update resource = nil
  form = form_const.new(resource || resource_klass.find(params[:id]))
  if form.validate(params) && form.save
    resource = form.model
    instance_variable_set("@#{resource_name(resource)}", resource)
    if block_given?
      yield resource
    else
      render json: { msg: :successfully_update }, status: 200
    end
  else
    render json: { msg: form.errors.full_messages.first || form.model.errors.full_messages.first }, status: 422
  end
end
Also aliased as: br_update

Protected Instance Methods

destroy_resource(id = nil) click to toggle source
# File lib/base_resource/actions.rb, line 95
def destroy_resource(id = nil)
  id ||= respond_to?(:params) && params.is_a?(ActionController::Parameters) && params[:id]
  resource_klass.destroy id
end
find_base_resource(id = nil) click to toggle source
# File lib/base_resource/actions.rb, line 90
def find_base_resource(id = nil)
  id ||= respond_to?(:params) && params.is_a?(ActionController::Parameters) && params[:id]
  resource_klass.find id
end
find_base_resources(resources = nil) click to toggle source
# File lib/base_resource/actions.rb, line 84
def find_base_resources resources = nil
  search = (resources || resource_klass).ransack(prepare_search_condition)
  search.sorts = prepare_search_sorts if search.sorts.empty? && prepare_search_sorts.present?
  search.result(distinct: true)
end
form_const() click to toggle source
# File lib/base_resource/actions.rb, line 100
def form_const
  namespaces = self.class.to_s.split("::")
  const = while namespaces.present?
            const = "#{namespaces.join('::')}::#{resource_klass_name}Form::#{action_name.classify}".constantize rescue nil
            break const if const
            namespaces.pop
          end
  const || "#{resource_klass_name}Form::#{action_name.classify}".try(:constantize)
end