Class: RivetCms::ComponentsController

Inherits:
ApplicationController show all
Includes:
InertiaProps
Defined in:
app/controllers/rivet_cms/components_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/rivet_cms/components_controller.rb', line 31

def create
  component = Component.new(component_params)

  if component.save
    audit "component.created", component
    redirect_to component_path(component), notice: "Component created successfully"
  else
    redirect_to new_component_path, inertia: { errors: component.errors }
  end
end

#create_categoryObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/rivet_cms/components_controller.rb', line 71

def create_category
  category = Category.new(category_params)
  category.slug = category.name.to_s.parameterize if category.slug.blank?
  category.system = false if category.system.nil?

  if category.save
    audit "category.created", category
    render json: { id: category.id, name: category.name }
  else
    render json: { errors: category.errors.full_messages }, status: :unprocessable_entity
  end
end

#destroyObject



63
64
65
66
67
68
69
# File 'app/controllers/rivet_cms/components_controller.rb', line 63

def destroy
  @component.destroy
  audit "component.deleted", @component
  redirect_to components_path, notice: "Component deleted"
rescue ActiveRecord::InvalidForeignKey
  redirect_to components_path, alert: "Component is in use and cannot be deleted"
end

#indexObject



14
15
16
17
18
19
20
21
22
# File 'app/controllers/rivet_cms/components_controller.rb', line 14

def index
  field_counts = Field.where(component_id: Current.organization.components.select(:id)).group(:component_id).count

  render inertia: "Components/Index", props: {
    components: permitted(Current.organization.components.includes(:category), :read, :schema).map { |c|
      component_props(c).merge(fields_count: field_counts.fetch(c.id, 0))
    }
  }
end

#newObject



24
25
26
27
28
29
# File 'app/controllers/rivet_cms/components_controller.rb', line 24

def new
  render inertia: "Components/New", props: {
    categories: Current.organization.categories.order(:name).map { |c| category_props(c) },
    create_category_path: create_category_components_path
  }
end

#showObject



42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/rivet_cms/components_controller.rb', line 42

def show
  render inertia: "Components/Show", props: {
    component: component_props(@component),
    categories: Current.organization.categories.order(:name).map { |c| category_props(c) },
    create_category_path: create_category_components_path,
    fields: @component.fields.kept.ordered.map { |f| field_props(f) },
    field_types: Field::FIELD_TYPE_LABELS.except("component"),
    reference_targets: permitted(Current.organization.content_types.order(:name), :read, :schema).map { |ct| { id: ct.id, name: ct.name } },
    embeddable_components: []
  }
end

#updateObject



54
55
56
57
58
59
60
61
# File 'app/controllers/rivet_cms/components_controller.rb', line 54

def update
  if @component.update(component_params)
    audit "component.updated", @component
    redirect_to component_path(@component), notice: "Component updated successfully"
  else
    redirect_to component_path(@component), inertia: { errors: @component.errors }
  end
end