decidim-restfull-blogs 0.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/app/controllers/decidim/api/rest_full/blogs/blogs_controller.rb +174 -0
- data/app/controllers/decidim/api/rest_full/components/blog_components_controller.rb +71 -0
- data/app/serializers/decidim/api/rest_full/blogs/blog_component_serializer.rb +25 -0
- data/app/serializers/decidim/api/rest_full/blogs/blog_serializer.rb +70 -0
- data/app/serializers/decidim/api/rest_full/blogs/post_serializer.rb +12 -0
- data/app/services/decidim/rest_full/blogs/blogs_operations.rb +108 -0
- data/config/locales/decidim_rest_full_blogs.en.yml +13 -0
- data/lib/decidim/rest_full/blogs/engine.rb +55 -0
- data/lib/decidim/rest_full/blogs/test_definitions.rb +6 -0
- data/lib/decidim/rest_full/test/definitions/blog.rb +112 -0
- data/lib/decidim/rest_full/test/definitions/blog_component_schema.rb +11 -0
- data/lib/decidim/rest_full/test/definitions/blogs.rb +3 -0
- data/lib/decidim/rest_full/test/definitions/tags/blog_tag.rb +26 -0
- data/lib/decidim-restfull-blogs.rb +13 -0
- data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_create_spec.rb +83 -0
- data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_index_spec.rb +168 -0
- data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_show_pagination_spec.rb +125 -0
- data/spec/requests/decidim/api/rest_full/blogs/blogs_controller_show_spec.rb +234 -0
- data/spec/requests/decidim/api/rest_full/components/blog_components_controller_index_spec.rb +79 -0
- data/spec/requests/decidim/api/rest_full/components/blog_components_controller_show_spec.rb +54 -0
- data/spec/requests/decidim/api/rest_full/scenarios/blog_published_at_desc_prev_next_chain_spec.rb +71 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9bdc9ca779372a76024df347656efd836654ce6e8007bdb666d9063d8c05e88e
|
|
4
|
+
data.tar.gz: a73901312f795249b8850242c3fcda59d3107e0a0e8a2a9c35608481f629348f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0df79c35bfb092603ec763f491079c3a915e43e1393a27d47130d19a96709a3faa6e2a9e8792cc919ec8fa5215ec9a51fff531e15d65d80d382b1bbbde8a0721
|
|
7
|
+
data.tar.gz: d75bd6a42da7dac9eb2ae127ac473751e2af5f5319d3dd30a99dfedfa1b1856a9899d4797e3b49043b6a33d7eb897042aa70507a5f1ee9ea516e2e283a9307dd
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Blogs
|
|
7
|
+
class BlogsController < Decidim::Api::RestFull::Core::ResourcesController
|
|
8
|
+
include Decidim::Api::RestFull::AsyncApiJobEnqueuing
|
|
9
|
+
|
|
10
|
+
before_action { doorkeeper_authorize! :blogs }
|
|
11
|
+
before_action :authorize_read!, only: [:index, :show]
|
|
12
|
+
before_action :authorize_create!, only: [:create, :create_sync]
|
|
13
|
+
before_action :authorize_update!, only: [:update, :update_sync]
|
|
14
|
+
before_action :authorize_destroy!, only: [:destroy, :destroy_sync]
|
|
15
|
+
|
|
16
|
+
def index
|
|
17
|
+
scoped = collection.includes(:component)
|
|
18
|
+
records, meta = paginate_collection(scoped)
|
|
19
|
+
payload = Decidim::Api::RestFull::Blogs::BlogSerializer.new(
|
|
20
|
+
records,
|
|
21
|
+
params: serializer_params
|
|
22
|
+
).serializable_hash
|
|
23
|
+
payload[:meta] = meta
|
|
24
|
+
render_json_with_conditional_get(payload, fingerprint: collection_fingerprint_for(scoped))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def show
|
|
28
|
+
@resource = find_resource!
|
|
29
|
+
render_json_with_conditional_get(
|
|
30
|
+
serialized_show(@resource),
|
|
31
|
+
fingerprint: conditional_get_fingerprint
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def create
|
|
36
|
+
enqueue_rest_full_api_job!("blogs/posts#create")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def create_sync
|
|
40
|
+
render json: Decidim::RestFull::SyncRunner.call do
|
|
41
|
+
Decidim::RestFull::Blogs::BlogsOperations.new(api_execution_context, params).create!
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def update
|
|
46
|
+
enqueue_rest_full_api_job!("blogs/posts#update")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def update_sync
|
|
50
|
+
render json: Decidim::RestFull::SyncRunner.call do
|
|
51
|
+
Decidim::RestFull::Blogs::BlogsOperations.new(api_execution_context, params).update!
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def destroy
|
|
56
|
+
enqueue_rest_full_api_job!("blogs/posts#destroy")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def destroy_sync
|
|
60
|
+
render json: Decidim::RestFull::SyncRunner.call do
|
|
61
|
+
Decidim::RestFull::Blogs::BlogsOperations.new(api_execution_context, params).destroy!
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def serialized_show(resource = find_resource!)
|
|
68
|
+
pagination = pagination_meta
|
|
69
|
+
Decidim::Api::RestFull::Blogs::BlogSerializer.new(
|
|
70
|
+
resource,
|
|
71
|
+
params: serializer_params.merge(pagination)
|
|
72
|
+
).serializable_hash
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def order_columns
|
|
76
|
+
%w(rand published_at)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def default_order_column
|
|
80
|
+
"published_at"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def default_order_direction
|
|
84
|
+
"desc"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def collection
|
|
88
|
+
query = filter_for_context(model_class.order(published_at: :asc))
|
|
89
|
+
query = query.where(decidim_component_id: params.require(:component_id)) if params.has_key? :component_id
|
|
90
|
+
|
|
91
|
+
now = Time.zone.now
|
|
92
|
+
table = model_class.table_name
|
|
93
|
+
query = if act_as.nil?
|
|
94
|
+
query.where(table => { published_at: ...now })
|
|
95
|
+
else
|
|
96
|
+
query.where(
|
|
97
|
+
"#{table}.published_at <= ? OR (#{table}.published_at > ? AND #{table}.decidim_author_id = ?)",
|
|
98
|
+
now,
|
|
99
|
+
now,
|
|
100
|
+
act_as.id
|
|
101
|
+
)
|
|
102
|
+
end
|
|
103
|
+
ordered(query)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def serializer_params
|
|
107
|
+
{
|
|
108
|
+
only: [],
|
|
109
|
+
locales: available_locales,
|
|
110
|
+
host: current_organization.host,
|
|
111
|
+
act_as:
|
|
112
|
+
}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def find_resource!
|
|
116
|
+
resource = collection.find_by(id: resource_id)
|
|
117
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Post not found" unless resource
|
|
118
|
+
|
|
119
|
+
resource
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def pagination_meta
|
|
123
|
+
match = pagination_match
|
|
124
|
+
bounds = collection_pagination_bounds(collection)
|
|
125
|
+
{
|
|
126
|
+
first: bounds[:first],
|
|
127
|
+
last: bounds[:last],
|
|
128
|
+
next: match&.next_id,
|
|
129
|
+
prev: match&.previous_id,
|
|
130
|
+
count: bounds[:count]
|
|
131
|
+
}
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def pagination_match
|
|
135
|
+
subquery = collection
|
|
136
|
+
.select(:id, "LAG(id) OVER (ORDER BY #{order_string}) AS previous_id", "LEAD(id) OVER (ORDER BY #{order_string}) AS next_id")
|
|
137
|
+
.to_sql
|
|
138
|
+
aliased = "(#{subquery}) AS subquery"
|
|
139
|
+
# SoftDeletable/paranoia would otherwise add deleted_at on the outer
|
|
140
|
+
# relation, but the subquery alias has no such column.
|
|
141
|
+
model_class.unscoped
|
|
142
|
+
.select("subquery.id, subquery.previous_id as previous_id, subquery.next_id as next_id")
|
|
143
|
+
.from(aliased)
|
|
144
|
+
.find_by("subquery.id = ? ", resource_id)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def model_class
|
|
148
|
+
Decidim::Blogs::Post
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def component_manifest
|
|
152
|
+
"blogs"
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def authorize_read!
|
|
156
|
+
ability.authorize! :read, ::Decidim::Blogs::Post
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def authorize_create!
|
|
160
|
+
ability.authorize! :create, ::Decidim::Blogs::Post
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def authorize_update!
|
|
164
|
+
ability.authorize! :update, ::Decidim::Blogs::Post
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def authorize_destroy!
|
|
168
|
+
ability.authorize! :destroy, ::Decidim::Blogs::Post
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Components
|
|
7
|
+
class BlogComponentsController < Decidim::Api::RestFull::Components::ComponentsController
|
|
8
|
+
before_action { doorkeeper_authorize! :public }
|
|
9
|
+
before_action { ability.authorize! :read, ::Decidim::Component }
|
|
10
|
+
|
|
11
|
+
def index
|
|
12
|
+
query = collection.reorder(nil).order(id: :asc).ransack(params[:filter]).result
|
|
13
|
+
scoped = in_visible_spaces(query)
|
|
14
|
+
records, meta = paginate_collection(scoped)
|
|
15
|
+
data = records.map do |component|
|
|
16
|
+
Decidim::Api::RestFull::Blogs::BlogComponentSerializer.new(
|
|
17
|
+
component,
|
|
18
|
+
params: { only: [], locales: available_locales, host: current_organization.host, act_as: }
|
|
19
|
+
).serializable_hash[:data]
|
|
20
|
+
end
|
|
21
|
+
fp = Decidim::RestFull::Core::HttpCache::CollectionFingerprint.for_request(self, relation: scoped)
|
|
22
|
+
render_json_with_conditional_get({ data:, meta: }, fingerprint: fp)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def show
|
|
26
|
+
resource_id = params.require(:id).to_i
|
|
27
|
+
match = collection.find(resource_id)
|
|
28
|
+
payload = Decidim::Api::RestFull::Blogs::BlogComponentSerializer.new(
|
|
29
|
+
match,
|
|
30
|
+
params: { only: [], locales: available_locales, host: current_organization.host, act_as: }
|
|
31
|
+
).serializable_hash
|
|
32
|
+
render_json_with_conditional_get(
|
|
33
|
+
payload,
|
|
34
|
+
fingerprint: Decidim::RestFull::Core::HttpCache::ResourceShowFingerprint.for_request(self, match)
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
protected
|
|
39
|
+
|
|
40
|
+
def blog
|
|
41
|
+
@blog ||= collection.find(params.require(:resource_id))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def blog_component
|
|
45
|
+
@blog_component ||= blog.component
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def order_columns
|
|
49
|
+
%w(rand published_at)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def default_order_column
|
|
53
|
+
"published_at"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def component_manifest
|
|
57
|
+
"blogs"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def model_class
|
|
61
|
+
Decidim::Component
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def collection
|
|
65
|
+
model_class.where(manifest_name: "blogs")
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Blogs
|
|
7
|
+
class BlogComponentSerializer < ::Decidim::Api::RestFull::Core::ComponentSerializer
|
|
8
|
+
def self.resources_for(component, act_as)
|
|
9
|
+
resources = ::Decidim::Blogs::Post.where(component:)
|
|
10
|
+
if act_as.nil?
|
|
11
|
+
resources.published
|
|
12
|
+
else
|
|
13
|
+
resources.published.or(resources.where(published_at: nil, decidim_author_id: act_as.id))
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
has_many :resources, meta: (proc do |component, params|
|
|
17
|
+
{ count: resources_for(component, params[:act_as]).count }
|
|
18
|
+
end) do |component, params|
|
|
19
|
+
resources_for(component, params[:act_as]).limit(50)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module Api
|
|
5
|
+
module RestFull
|
|
6
|
+
module Blogs
|
|
7
|
+
class BlogSerializer < ::Decidim::Api::RestFull::Core::ResourceSerializer
|
|
8
|
+
extend ::Decidim::Api::RestFull::Core::Helpers::ResourceLinksHelper
|
|
9
|
+
|
|
10
|
+
def self.default_meta(blog_post)
|
|
11
|
+
scope = blog_post.component.scope || blog_post.participatory_space.scope
|
|
12
|
+
metas = {
|
|
13
|
+
published: blog_post.published?
|
|
14
|
+
}
|
|
15
|
+
metas[:scope] = scope.id if scope
|
|
16
|
+
metas
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
link :next, if: (proc do |_object, params|
|
|
20
|
+
params.has_key?(:next) && params[:next]
|
|
21
|
+
end) do |object, params|
|
|
22
|
+
next_id = params[:next]
|
|
23
|
+
infos = link_infos_from_resource(object)
|
|
24
|
+
{
|
|
25
|
+
href: link_for_resource(params[:host], infos, next_id),
|
|
26
|
+
title: "Next post",
|
|
27
|
+
rel: "resource",
|
|
28
|
+
meta: {
|
|
29
|
+
**infos,
|
|
30
|
+
resource_id: next_id.to_s,
|
|
31
|
+
action_method: "GET"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
link :prev, if: (proc do |_object, params|
|
|
37
|
+
params.has_key?(:prev) && params[:prev]
|
|
38
|
+
end) do |object, params|
|
|
39
|
+
prev_id = params[:prev]
|
|
40
|
+
infos = link_infos_from_resource(object)
|
|
41
|
+
{
|
|
42
|
+
href: link_for_resource(params[:host], infos, prev_id),
|
|
43
|
+
title: "Previous post",
|
|
44
|
+
rel: "resource",
|
|
45
|
+
meta: {
|
|
46
|
+
**infos,
|
|
47
|
+
resource_id: prev_id.to_s,
|
|
48
|
+
action_method: "GET"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
meta do |blog_post, _params|
|
|
54
|
+
default_meta(blog_post)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
attribute :title do |comp, params|
|
|
58
|
+
translated_field(comp.title, params[:locales])
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
attribute :body do |comp, params|
|
|
62
|
+
translated_field(comp.body, params[:locales])
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
attribute :published_at, &:published_at
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Blogs
|
|
6
|
+
class BlogsOperations
|
|
7
|
+
def initialize(execution_context, params)
|
|
8
|
+
@ctx = execution_context
|
|
9
|
+
@params = params.is_a?(ActionController::Parameters) ? params : ActionController::Parameters.new(params)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def create!
|
|
13
|
+
component = find_component!
|
|
14
|
+
authorize_manage!(component)
|
|
15
|
+
post = nil
|
|
16
|
+
Decidim::Blogs::Admin::CreatePost.call(build_form(component)) do
|
|
17
|
+
on(:ok) { |resource| post = resource }
|
|
18
|
+
on(:invalid) { raise Decidim::RestFull::Core::ApiException::BadRequest, "Post is invalid" }
|
|
19
|
+
end
|
|
20
|
+
serialize(post)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def update!
|
|
24
|
+
post = find_post!
|
|
25
|
+
authorize_manage!(post.component)
|
|
26
|
+
attrs = data_attributes.permit(:published_at, title: {}, body: {}).to_h
|
|
27
|
+
post.update!(attrs) if attrs.present?
|
|
28
|
+
serialize(post.reload)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def destroy!
|
|
32
|
+
post = find_post!
|
|
33
|
+
authorize_manage!(post.component)
|
|
34
|
+
serialized = serialize(post)
|
|
35
|
+
post.destroy!
|
|
36
|
+
serialized
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
attr_reader :ctx, :params
|
|
42
|
+
|
|
43
|
+
delegate :organization, :current_user, :available_locales, to: :ctx
|
|
44
|
+
|
|
45
|
+
def data_attributes
|
|
46
|
+
data = params["data"] || params[:data] || {}
|
|
47
|
+
data = data.to_unsafe_h if data.respond_to?(:to_unsafe_h)
|
|
48
|
+
attrs = data["attributes"] || data[:attributes] || {}
|
|
49
|
+
attrs = attrs.to_unsafe_h if attrs.respond_to?(:to_unsafe_h)
|
|
50
|
+
ActionController::Parameters.new(attrs)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def build_form(component)
|
|
54
|
+
attrs = data_attributes.permit(:published_at, title: {}, body: {}).to_h
|
|
55
|
+
Decidim::Blogs::Admin::PostForm.from_params(
|
|
56
|
+
{
|
|
57
|
+
title: attrs["title"] || {},
|
|
58
|
+
body: attrs["body"] || {},
|
|
59
|
+
published_at: attrs["published_at"],
|
|
60
|
+
decidim_author_id: current_user.id
|
|
61
|
+
},
|
|
62
|
+
current_user:
|
|
63
|
+
).with_context(current_component: component, current_organization: organization)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def find_component!
|
|
67
|
+
component_id = params.dig("data", "component_id") || params.dig(:data, :component_id) || params[:component_id]
|
|
68
|
+
raise Decidim::RestFull::Core::ApiException::BadRequest, "component_id required" if component_id.blank?
|
|
69
|
+
|
|
70
|
+
component = Decidim::Component.find_by(id: component_id, manifest_name: "blogs")
|
|
71
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Component not found" unless component
|
|
72
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Component not found" unless component.organization == organization
|
|
73
|
+
|
|
74
|
+
component
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def find_post!
|
|
78
|
+
Decidim::Blogs::Post.find_by!(id: resource_id, decidim_component_id: visible_blog_component_ids)
|
|
79
|
+
rescue ActiveRecord::RecordNotFound
|
|
80
|
+
raise Decidim::RestFull::Core::ApiException::NotFound, "Post not found"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def resource_id
|
|
84
|
+
(params[:id] || params["id"]).to_s
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def visible_blog_component_ids
|
|
88
|
+
Decidim::Component.where(manifest_name: "blogs", organization:).pluck(:id)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def authorize_manage!(component)
|
|
92
|
+
raise Decidim::RestFull::Core::ApiException::Forbidden, "Not allowed" unless component.organization == organization
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def serialize(post)
|
|
96
|
+
Decidim::Api::RestFull::Blogs::BlogSerializer.new(
|
|
97
|
+
post,
|
|
98
|
+
params: {
|
|
99
|
+
locales: available_locales,
|
|
100
|
+
host: organization.host,
|
|
101
|
+
act_as: current_user
|
|
102
|
+
}
|
|
103
|
+
).serializable_hash
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
en:
|
|
3
|
+
decidim:
|
|
4
|
+
rest_full:
|
|
5
|
+
models:
|
|
6
|
+
api_client:
|
|
7
|
+
fields:
|
|
8
|
+
scope_blogs: 'Blogs: manage posts (Blogs::Post)'
|
|
9
|
+
permission:
|
|
10
|
+
blogs:
|
|
11
|
+
destroy: 'destroy: delete a post'
|
|
12
|
+
read: 'read: list and details of a post'
|
|
13
|
+
write: 'write: create and update posts'
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Decidim
|
|
4
|
+
module RestFull
|
|
5
|
+
module Blogs
|
|
6
|
+
class Engine < ::Rails::Engine
|
|
7
|
+
config.root = Blogs::ENGINE_ROOT
|
|
8
|
+
|
|
9
|
+
initializer "rest_full.blogs.extension" do
|
|
10
|
+
Decidim::RestFull::Extension.register(:blogs) do |ext|
|
|
11
|
+
ext.toggle_feature gem: "decidim-restfull-blogs"
|
|
12
|
+
ext.controller_paths "blogs", "blog_components"
|
|
13
|
+
ext.oauth_scopes :blogs
|
|
14
|
+
ext.permissions(:blogs, "blogs.read", group: :blogs)
|
|
15
|
+
ext.permissions(:blogs, "blogs.write", group: :blogs)
|
|
16
|
+
ext.permissions(:blogs, "blogs.destroy", group: :blogs)
|
|
17
|
+
|
|
18
|
+
ext.api_job "blogs/posts#create", ->(ctx, p) { Blogs::BlogsOperations.new(ctx, p).create! }
|
|
19
|
+
ext.api_job "blogs/posts#update", ->(ctx, p) { Blogs::BlogsOperations.new(ctx, p).update! }
|
|
20
|
+
ext.api_job "blogs/posts#destroy", ->(ctx, p) { Blogs::BlogsOperations.new(ctx, p).destroy! }
|
|
21
|
+
|
|
22
|
+
ext.open_api_definitions(
|
|
23
|
+
File.join(Blogs::ENGINE_ROOT, "lib/decidim/rest_full/blogs/test_definitions.rb")
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
ext.rswag_specs(
|
|
27
|
+
File.join(Blogs::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/blogs/**/*_spec.rb"),
|
|
28
|
+
File.join(Blogs::ENGINE_ROOT, "spec/requests/decidim/api/rest_full/components/blog_components*_spec.rb")
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
ext.routes do
|
|
32
|
+
resources :components, only: [] do
|
|
33
|
+
collection do
|
|
34
|
+
Decidim::RestFull::Routing.read_resources(
|
|
35
|
+
self,
|
|
36
|
+
:blog_components,
|
|
37
|
+
controller: "components/blog_components",
|
|
38
|
+
only: [:index, :show]
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
Decidim::RestFull::Routing.async_resources(
|
|
44
|
+
self,
|
|
45
|
+
:blogs,
|
|
46
|
+
controller: "blogs/blogs",
|
|
47
|
+
only: [:index, :show, :create, :update, :destroy]
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_resource(:blog) do
|
|
4
|
+
{
|
|
5
|
+
type: :object,
|
|
6
|
+
title: "Post",
|
|
7
|
+
properties: {
|
|
8
|
+
id: { type: :string, description: "Post id (Blogs::Post)" },
|
|
9
|
+
type: { type: :string, enum: ["blog"], description: "JSON:API type discriminator for this resource" },
|
|
10
|
+
attributes: {
|
|
11
|
+
type: :object,
|
|
12
|
+
title: "Post attributes",
|
|
13
|
+
properties: {
|
|
14
|
+
title: {
|
|
15
|
+
"$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop)
|
|
16
|
+
},
|
|
17
|
+
body: {
|
|
18
|
+
"$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop)
|
|
19
|
+
},
|
|
20
|
+
created_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:creation_date) },
|
|
21
|
+
updated_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:edition_date) },
|
|
22
|
+
published_at: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:publication_date) }
|
|
23
|
+
},
|
|
24
|
+
required: [:created_at, :updated_at, :title, :body],
|
|
25
|
+
additionalProperties: false
|
|
26
|
+
},
|
|
27
|
+
meta: {
|
|
28
|
+
type: :object,
|
|
29
|
+
title: "Post metadata",
|
|
30
|
+
properties: {
|
|
31
|
+
published: { type: :boolean, description: "Whether the post is published" },
|
|
32
|
+
scope: { type: :integer, description: "Scope id (component or participatory space scope)" }
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: {
|
|
35
|
+
oneOf: [
|
|
36
|
+
{
|
|
37
|
+
type: :boolean
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
type: :integer
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
type: :string
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop)
|
|
47
|
+
}
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
required: [:published]
|
|
51
|
+
},
|
|
52
|
+
links: {
|
|
53
|
+
type: :object,
|
|
54
|
+
title: "Post links",
|
|
55
|
+
properties: {
|
|
56
|
+
self: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
|
|
57
|
+
collection: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
|
|
58
|
+
related: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
|
|
59
|
+
prev: Decidim::RestFull::Core::DefinitionRegistry.resource_link,
|
|
60
|
+
next: Decidim::RestFull::Core::DefinitionRegistry.resource_link
|
|
61
|
+
},
|
|
62
|
+
additionalProperties: false,
|
|
63
|
+
required: [:self, :collection, :related]
|
|
64
|
+
},
|
|
65
|
+
relationships: {
|
|
66
|
+
type: :object,
|
|
67
|
+
title: "Post relationships",
|
|
68
|
+
properties: {
|
|
69
|
+
space: Decidim::RestFull::Core::DefinitionRegistry.belongs_to(*Decidim.participatory_space_registry.manifests.map(&:name), title: "Linked participatory space"),
|
|
70
|
+
component: Decidim::RestFull::Core::DefinitionRegistry.belongs_to("blog_component", title: "Linked blogs component")
|
|
71
|
+
},
|
|
72
|
+
required: [:component, :space],
|
|
73
|
+
additionalProperties: false
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
required: [:id, :type, :attributes, :meta, :links]
|
|
77
|
+
}.freeze
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_object(:blog_post_create_payload) do
|
|
81
|
+
{
|
|
82
|
+
type: :object,
|
|
83
|
+
title: "Blog post create payload",
|
|
84
|
+
required: [:data],
|
|
85
|
+
properties: {
|
|
86
|
+
data: {
|
|
87
|
+
type: :object,
|
|
88
|
+
required: [:component_id, :attributes],
|
|
89
|
+
properties: {
|
|
90
|
+
component_id: { type: :integer, description: "Blogs component id" },
|
|
91
|
+
attributes: {
|
|
92
|
+
type: :object,
|
|
93
|
+
required: [:title, :body],
|
|
94
|
+
properties: {
|
|
95
|
+
title: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) },
|
|
96
|
+
body: { "$ref" => Decidim::RestFull::Core::DefinitionRegistry.reference(:translated_prop) },
|
|
97
|
+
published_at: {
|
|
98
|
+
type: :string,
|
|
99
|
+
format: :"date-time",
|
|
100
|
+
nullable: true,
|
|
101
|
+
description: "ISO8601; omit or null for draft"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
additionalProperties: false
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
additionalProperties: false
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
additionalProperties: false
|
|
111
|
+
}.freeze
|
|
112
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_component_manifest_schema(manifest: :blogs, schema_name: :blog_component)
|
|
4
|
+
|
|
5
|
+
Decidim::RestFull::Core::DefinitionRegistry.extends_object(:blog_component, :generic_component) do |blog_component|
|
|
6
|
+
blog_component[:title] = "Blogs component"
|
|
7
|
+
blog_component[:properties][:type] = { type: :string, enum: ["blog_component"] }
|
|
8
|
+
blog_component[:properties][:attributes][:properties][:manifest_name] = { type: :string, enum: ["blogs"] }
|
|
9
|
+
blog_component
|
|
10
|
+
end
|
|
11
|
+
Decidim::RestFull::Core::DefinitionRegistry.register_response_for(:blog_component)
|