Module: BaseResource::Actions

Includes:
InstanceMethods, Pagination, Search
Included in:
BaseResource
Defined in:
lib/base_resource/actions.rb

Instance Method Summary collapse

Methods included from InstanceMethods

#resource_klass, #resource_klass_name, #resource_name, #resources_name

Instance Method Details

#create(options = {}) ⇒ Object Also known as: br_create



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

#destroy ⇒ Object Also known as: br_destroy



71
72
73
74
75
76
77
78
79
# 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

#index(resources = nil) ⇒ Object Also known as: br_index



10
11
12
13
14
15
16
# 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

#show ⇒ Object Also known as: br_show

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



21
22
23
24
25
26
27
# 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

#update(resource = nil) ⇒ Object Also known as: br_update



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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