Module: Comee::Core::Common

Instance Method Summary collapse

Methods included from Pagination

#default_per_page, #order_by, #order_direction, #page_no, #paginate, #paginate_offset, #per_page

Instance Method Details

#createObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/comee/core/common.rb', line 64

def create
  obj = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      obj, options = incoming
    elsif incoming.instance_of?(Hash)
      obj = @clazz.new(model_params)
      options = incoming
    else
      obj = incoming
    end
  else
    obj = @clazz.new(model_params)
  end

  if obj.save
    result = {
      success: true,
      data: serialize(obj, options)
    }
    render json: result, status: :created
  else
    render json: {success: false, error: obj.errors.full_messages[0]}, status: 422
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end

#indexObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/concerns/comee/core/common.rb', line 12

def index
  data = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      data, options = incoming
    elsif incoming.instance_of?(Hash)
      options = incoming
    else
      data = incoming
    end
  else
    data = @clazz.all
  end
  total = data.count
  data = data.then(&paginate) if params[:page]
  result = {
    success: true,
    data: serialize(data, options)
  }
  if params[:page]
    result[:page] = params[:page]
    result[:total] = total
  end

  render json: result
end

#showObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/concerns/comee/core/common.rb', line 41

def show
  data = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      data, options = incoming
    elsif incoming.instance_of?(Hash)
      data = @obj
      options = incoming
    else
      data = incoming
    end
  else
    data = @obj
  end
  result = {
    success: true,
    data: serialize(data, options)
  }
  render json: result
end

#updateObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/comee/core/common.rb', line 94

def update
  obj = nil
  options = {}
  if block_given?
    incoming = yield
    if incoming.instance_of?(Array)
      obj, options = incoming
    elsif incoming.instance_of?(Hash)
      obj = set_object
      options = incoming
    else
      obj = incoming
    end
  else
    obj = set_object
  end

  if obj.update(model_params)
    result = {
      success: true,
      data: serialize(obj, options)
    }
    render json: result
  else
    render json: {success: false, error: obj.errors.full_messages[0]}, status: 422
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end