Class: Decidim::Admin::ComponentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/decidim/admin/components_controller.rb

Overview

Controller that allows managing the Participatory Process’ Components in the admin panel.

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain, #permission_scope, #user_has_no_permission_path, #user_not_authorized_path

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/decidim/admin/components_controller.rb', line 32

def create
  @form = form(manifest.component_form_class).from_params(component_params)
  enforce_permission_to :create, :component

  CreateComponent.call(@form) do
    on(:ok) do
      flash[:notice] = I18n.t("components.create.success", scope: "decidim.admin")
      redirect_to action: :index
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("components.create.error", scope: "decidim.admin")
      render action: "new"
    end
  end
end

#destroyObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/decidim/admin/components_controller.rb', line 76

def destroy
  @component = query_scope.find(params[:id])
  enforce_permission_to :destroy, :component, component: @component

  DestroyComponent.call(@component, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("components.destroy.success", scope: "decidim.admin")
      redirect_to action: :index
    end

    on(:invalid) do
      flash[:alert] = I18n.t("components.destroy.error", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#editObject



49
50
51
52
53
54
# File 'app/controllers/decidim/admin/components_controller.rb', line 49

def edit
  @component = query_scope.find(params[:id])
  enforce_permission_to :update, :component, component: @component

  @form = form(@component.form_class).from_model(@component)
end

#indexObject



11
12
13
14
15
# File 'app/controllers/decidim/admin/components_controller.rb', line 11

def index
  enforce_permission_to :read, :component
  @manifests = Decidim.component_manifests
  @components = current_participatory_space.components
end

#newObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/decidim/admin/components_controller.rb', line 17

def new
  enforce_permission_to :create, :component

  @component = Component.new(
    name: default_name(manifest),
    manifest_name: params[:type],
    participatory_space: current_participatory_space,
    settings: {
      scope_id: current_participatory_space.scope.try(:id)
    }
  )

  @form = form(@component.form_class).from_model(@component)
end

#publishObject



93
94
95
96
97
98
99
100
101
102
103
# File 'app/controllers/decidim/admin/components_controller.rb', line 93

def publish
  @component = query_scope.find(params[:id])
  enforce_permission_to :publish, :component, component: @component

  PublishComponent.call(@component, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("components.publish.success", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#shareObject



117
118
119
120
121
122
# File 'app/controllers/decidim/admin/components_controller.rb', line 117

def share
  @component = query_scope.find(params[:id])
  share_token = @component.share_tokens.create!(user: current_user, organization: current_organization)

  redirect_to share_token.url
end

#unpublishObject



105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/decidim/admin/components_controller.rb', line 105

def unpublish
  @component = query_scope.find(params[:id])
  enforce_permission_to :unpublish, :component, component: @component

  UnpublishComponent.call(@component, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("components.unpublish.success", scope: "decidim.admin")
      redirect_to action: :index
    end
  end
end

#updateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/decidim/admin/components_controller.rb', line 56

def update
  @component = query_scope.find(params[:id])
  @form = form(@component.form_class).from_params(component_params)
  enforce_permission_to :update, :component, component: @component

  UpdateComponent.call(@form, @component, current_user) do
    on(:ok) do |settings_changed, previous_settings, current_settings|
      handle_component_settings_change(previous_settings, current_settings) if settings_changed

      flash[:notice] = I18n.t("components.update.success", scope: "decidim.admin")
      redirect_to action: :index
    end

    on(:invalid) do
      flash[:alert] = I18n.t("components.update.error", scope: "decidim.admin")
      render action: :edit
    end
  end
end