Module: TenaDcms::Common

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/tena_dcms/common.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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

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

#destroyObject



55
56
57
# File 'app/controllers/concerns/tena_dcms/common.rb', line 55

def destroy
  @obj.destroy
end

#indexObject



12
13
14
15
16
17
18
19
# File 'app/controllers/concerns/tena_dcms/common.rb', line 12

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

#showObject



21
22
23
# File 'app/controllers/concerns/tena_dcms/common.rb', line 21

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

#updateObject



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

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