-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpages.rb
52 lines (38 loc) · 1.3 KB
/
pages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Pages
extend ActiveSupport::Concern
included do
include SyncResourceTranslationStrings
resource_actions :default
schema_type :strong_params
allowed_params :create, :url_key, :title, :content, :language
allowed_params :update, :url_key, :title, :content, :language
before_action :set_language_if_missing, only: [:index]
# Methods defined here in order to avoid being overridden by resource modules
define_method(:link_header) do |resource|
resource = resource.first
send(:"api_#{ resource_name }_url", id: resource.id, "#{parent_resource}_id": resource_id)
end
define_method(:build_resource_for_create) do |create_params|
create_params[:links] ||= {}
create_params[:links][parent_resource] = resource_id
super create_params
end
require_authentication :update, :create, :destroy, scopes: [:"#{self::PARENT_RESOURCE}"]
end
def controlled_resources
@controlled_resouces ||= super.where("#{parent_resource}": resource_id)
end
def parent_resource
self.class::PARENT_RESOURCE
end
def resource_id
params[:"#{parent_resource}_id"]
end
protected
def set_language_if_missing
params[:language] ||= "en"
end
def serializer
@serializer ||= "#{resource_name.camelize}Serializer".constantize
end
end