Class: MetaFieldApi::MetafieldsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/meta_field_api/metafields_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Store metafields



16
17
18
19
20
21
22
23
24
25
26
# File 'app/controllers/meta_field_api/metafields_controller.rb', line 16

def create
  metafield = Metafield.new(metafield_params)

  if metafield.save
    render json: { metafield: metafield, message: 'Metafield created successfully!' }, status: :created
  else
    render json: { error: metafield.errors }, status: :unprocessable_entity
    end
rescue StandardError => e
  render json: { error: e.message }, status: :unprocessable_entity
end

#destroyObject



52
53
54
55
56
57
58
59
60
# File 'app/controllers/meta_field_api/metafields_controller.rb', line 52

def destroy
  metafield = Metafield.find_by(id: params[:id])

  if metafield&.destroy
    render json: { message: 'Metafield deleted successfully!' }, status: :ok
  else
    render json: { error: 'Metafield not found or could not be deleted' }, status: :unprocessable_entity
  end
end

#indexObject

List of all metafields



4
5
6
7
8
9
10
11
12
13
# File 'app/controllers/meta_field_api/metafields_controller.rb', line 4

def index
  # Get the filter parameters from the params hash
  owner_type = params[:owner_type].presence
  owner_id = params[:owner_id].presence
  key = params[:key].presence
  namespace = params[:namespace].presence

  metafields = Metafield.filter_by(owner_type, owner_id, key, namespace)
  render json: { metafields: metafields, message: 'Metafields retrieved successfully!' }, status: :ok
end

#showObject

Show specific metafield



29
30
31
32
33
34
35
36
37
# File 'app/controllers/meta_field_api/metafields_controller.rb', line 29

def show
  metafield = Metafield.find_by(id: params[:id])

  if metafield
    render json: { metafield: metafield, message: 'Metafield retrieved successfully!' }, status: :ok
  else
    render json: { message: 'Metafield not found' }, status: :not_found
  end
end

#updateObject

Update specific metafield



40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/meta_field_api/metafields_controller.rb', line 40

def update
  metafield = Metafield.find_by(id: params[:id])

  if metafield.update(metafield_params)
    render json: { metafield: metafield, message: 'Metafield updated successfully!' }, status: :ok
  else
    render json: { error: metafield.errors }, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: { error: e.message }
end