Module: Cats::Core::Common

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/concerns/cats/core/common.rb', line 24

def create
  obj = if block_given?
          yield
        else
          @clazz.new(model_params)
        end
  if obj.save
    render json: {success: true, data: serialize(obj)}, status: :created
  else
    render json: {success: false, error: obj.errors.full_messages[0]}, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end

#indexObject



11
12
13
14
15
16
17
18
# File 'app/controllers/concerns/cats/core/common.rb', line 11

def index
  data = if block_given?
           yield
         else
           @clazz.all
         end
  render json: {success: true, data: serialize(data)}
end

#showObject



20
21
22
# File 'app/controllers/concerns/cats/core/common.rb', line 20

def show
  render json: {success: true, data: serialize(@obj)}
end

#updateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/concerns/cats/core/common.rb', line 39

def update
  obj = if block_given?
          yield
        else
          obj = @obj
        end
  if obj.update(model_params)
    render json: {success: true, data: serialize(obj)}
  else
    render json: {success: false, error: obj.errors.full_messages[0]}, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: {success: false, error: e.message}
end