lato_blog 2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +27 -0
  4. data/Rakefile +5 -0
  5. data/app/assets/config/lato_blog_manifest.js +2 -0
  6. data/app/assets/javascripts/lato_blog/application.js +19 -0
  7. data/app/assets/javascripts/lato_blog/initializers/Fields.js +56 -0
  8. data/app/assets/javascripts/lato_blog/initializers/Posts.js +54 -0
  9. data/app/assets/stylesheets/lato_blog/_helpers.scss +37 -0
  10. data/app/assets/stylesheets/lato_blog/application.scss +20 -0
  11. data/app/assets/stylesheets/lato_blog/fields/_composed.scss +7 -0
  12. data/app/assets/stylesheets/lato_blog/fields/_datetime.scss +0 -0
  13. data/app/assets/stylesheets/lato_blog/fields/_editor.scss +3 -0
  14. data/app/assets/stylesheets/lato_blog/fields/_fields.scss +8 -0
  15. data/app/assets/stylesheets/lato_blog/fields/_general.scss +9 -0
  16. data/app/assets/stylesheets/lato_blog/fields/_geolocalization.scss +3 -0
  17. data/app/assets/stylesheets/lato_blog/fields/_image.scss +3 -0
  18. data/app/assets/stylesheets/lato_blog/fields/_relay.scss +121 -0
  19. data/app/assets/stylesheets/lato_blog/fields/_text.scss +3 -0
  20. data/app/assets/stylesheets/lato_blog/pages/_categories.scss +18 -0
  21. data/app/assets/stylesheets/lato_blog/pages/_pages.scss +2 -0
  22. data/app/assets/stylesheets/lato_blog/pages/_posts.scss +25 -0
  23. data/app/assets/stylesheets/lato_blog/theme.scss.erb +12 -0
  24. data/app/assets/stylesheets/lato_blog/widgets/_switchlang.scss +3 -0
  25. data/app/assets/stylesheets/lato_blog/widgets/_widgets.scss +1 -0
  26. data/app/controllers/lato_blog/api/api_controller.rb +5 -0
  27. data/app/controllers/lato_blog/api/categories_controller.rb +51 -0
  28. data/app/controllers/lato_blog/api/posts_controller.rb +93 -0
  29. data/app/controllers/lato_blog/application_controller.rb +13 -0
  30. data/app/controllers/lato_blog/back/back_controller.rb +45 -0
  31. data/app/controllers/lato_blog/back/categories_controller.rb +141 -0
  32. data/app/controllers/lato_blog/back/post_fields_controller.rb +86 -0
  33. data/app/controllers/lato_blog/back/posts_controller.rb +340 -0
  34. data/app/controllers/lato_blog/doc/doc_controller.rb +16 -0
  35. data/app/controllers/lato_blog/doc/fields_controller.rb +19 -0
  36. data/app/controllers/lato_blog/doc/general_controller.rb +11 -0
  37. data/app/helpers/lato_blog/application_helper.rb +13 -0
  38. data/app/helpers/lato_blog/fields_helper.rb +100 -0
  39. data/app/jobs/lato_blog/application_job.rb +4 -0
  40. data/app/mailers/lato_blog/application_mailer.rb +6 -0
  41. data/app/models/lato_blog/application_record.rb +5 -0
  42. data/app/models/lato_blog/category.rb +119 -0
  43. data/app/models/lato_blog/category/entity_helpers.rb +55 -0
  44. data/app/models/lato_blog/category/serializer_helpers.rb +66 -0
  45. data/app/models/lato_blog/category_parent.rb +9 -0
  46. data/app/models/lato_blog/category_post.rb +35 -0
  47. data/app/models/lato_blog/post.rb +112 -0
  48. data/app/models/lato_blog/post/entity_helpers.rb +42 -0
  49. data/app/models/lato_blog/post/serializer_helpers.rb +85 -0
  50. data/app/models/lato_blog/post_field.rb +50 -0
  51. data/app/models/lato_blog/post_field/entity_helpers.rb +5 -0
  52. data/app/models/lato_blog/post_field/serializer_helpers.rb +127 -0
  53. data/app/models/lato_blog/post_parent.rb +20 -0
  54. data/app/views/lato_blog/back/back/switch_current_language.js +2 -0
  55. data/app/views/lato_blog/back/categories/edit.html.erb +44 -0
  56. data/app/views/lato_blog/back/categories/index.html.erb +18 -0
  57. data/app/views/lato_blog/back/categories/new.html.erb +40 -0
  58. data/app/views/lato_blog/back/categories/shared/_form.html.erb +38 -0
  59. data/app/views/lato_blog/back/categories/shared/edit/_edit_child_tree.html.erb +23 -0
  60. data/app/views/lato_blog/back/categories/shared/edit/_edit_child_tree_level.html.erb +7 -0
  61. data/app/views/lato_blog/back/categories/shared/edit/_edit_informations.html.erb +24 -0
  62. data/app/views/lato_blog/back/categories/shared/edit/_edit_languages.html.erb +38 -0
  63. data/app/views/lato_blog/back/categories/shared/new/_new_informations.html.erb +21 -0
  64. data/app/views/lato_blog/back/post_fields/create_relay_field.js.erb +14 -0
  65. data/app/views/lato_blog/back/post_fields/destroy_relay_field.js.erb +14 -0
  66. data/app/views/lato_blog/back/post_fields/fields/_composed.html.erb +24 -0
  67. data/app/views/lato_blog/back/post_fields/fields/_datetime.html.erb +17 -0
  68. data/app/views/lato_blog/back/post_fields/fields/_editor.html.erb +17 -0
  69. data/app/views/lato_blog/back/post_fields/fields/_geolocalization.html.erb +22 -0
  70. data/app/views/lato_blog/back/post_fields/fields/_image.html.erb +18 -0
  71. data/app/views/lato_blog/back/post_fields/fields/_relay.html.erb +39 -0
  72. data/app/views/lato_blog/back/post_fields/fields/_text.html.erb +17 -0
  73. data/app/views/lato_blog/back/post_fields/index.html.erb +28 -0
  74. data/app/views/lato_blog/back/posts/edit.html.erb +48 -0
  75. data/app/views/lato_blog/back/posts/index.html.erb +38 -0
  76. data/app/views/lato_blog/back/posts/new.html.erb +40 -0
  77. data/app/views/lato_blog/back/posts/shared/_fields.html.erb +13 -0
  78. data/app/views/lato_blog/back/posts/shared/_form.html.erb +69 -0
  79. data/app/views/lato_blog/back/posts/shared/edit/_edit_categories.html.erb +36 -0
  80. data/app/views/lato_blog/back/posts/shared/edit/_edit_informations.html.erb +26 -0
  81. data/app/views/lato_blog/back/posts/shared/edit/_edit_languages.html.erb +40 -0
  82. data/app/views/lato_blog/back/posts/shared/edit/_edit_pubblication_datetime.html.erb +24 -0
  83. data/app/views/lato_blog/back/posts/shared/edit/_edit_status_switch.html.erb +27 -0
  84. data/app/views/lato_blog/back/posts/shared/index/_index.html.erb +28 -0
  85. data/app/views/lato_blog/back/posts/shared/index/_index_informations.html.erb +37 -0
  86. data/app/views/lato_blog/back/posts/shared/index/_index_status_switch.html.erb +29 -0
  87. data/app/views/lato_blog/back/posts/shared/new/_new_informations.html.erb +21 -0
  88. data/app/views/lato_blog/doc/doc/index.html.erb +83 -0
  89. data/app/views/lato_blog/doc/fields/composed.html.erb +87 -0
  90. data/app/views/lato_blog/doc/fields/datetime.html.erb +0 -0
  91. data/app/views/lato_blog/doc/fields/editor.html.erb +1 -0
  92. data/app/views/lato_blog/doc/fields/geolocalization.html.erb +1 -0
  93. data/app/views/lato_blog/doc/fields/image.html.erb +70 -0
  94. data/app/views/lato_blog/doc/fields/relay.html.erb +87 -0
  95. data/app/views/lato_blog/doc/fields/text.html.erb +70 -0
  96. data/app/views/lato_blog/doc/general/api.html.erb +0 -0
  97. data/app/views/lato_blog/doc/general/installation.html.erb +49 -0
  98. data/app/views/lato_blog/doc/general/personalization.html.erb +0 -0
  99. data/app/views/lato_blog/partials/_relay_destroy_button.html.erb +5 -0
  100. data/app/views/lato_blog/widgets/_switchlang.html.erb +20 -0
  101. data/config/configs.yml +109 -0
  102. data/config/initializers/assets.rb +4 -0
  103. data/config/initializers/init_system.rb +22 -0
  104. data/config/languages/default.yml +87 -0
  105. data/config/languages/it.yml +87 -0
  106. data/config/routes.rb +16 -0
  107. data/config/routes/api.rb +11 -0
  108. data/config/routes/back.rb +17 -0
  109. data/config/routes/doc.rb +19 -0
  110. data/db/migrate/20170504222008_create_lato_blog_posts.rb +19 -0
  111. data/db/migrate/20170504223521_create_lato_blog_post_parents.rb +9 -0
  112. data/db/migrate/20170516063317_create_lato_blog_categories.rb +16 -0
  113. data/db/migrate/20170516063320_create_lato_blog_category_parents.rb +9 -0
  114. data/db/migrate/20170516063330_create_lato_blog_category_posts.rb +10 -0
  115. data/db/migrate/20170516063330_create_lato_blog_post_fields.rb +18 -0
  116. data/lib/lato_blog.rb +6 -0
  117. data/lib/lato_blog/engine.rb +15 -0
  118. data/lib/lato_blog/interface.rb +17 -0
  119. data/lib/lato_blog/interfaces/categories.rb +34 -0
  120. data/lib/lato_blog/interfaces/fields.rb +195 -0
  121. data/lib/lato_blog/interfaces/languages.rb +15 -0
  122. data/lib/lato_blog/interfaces/posts.rb +13 -0
  123. data/lib/lato_blog/version.rb +3 -0
  124. data/lib/tasks/lato_blog_tasks.rake +4 -0
  125. metadata +223 -0
@@ -0,0 +1,2 @@
1
+ @import "posts";
2
+ @import "categories";
@@ -0,0 +1,25 @@
1
+ .posts__edit-status-switch, .posts__edit-publication-datetime {
2
+
3
+ .elements-button {
4
+ margin-top: 0;
5
+ }
6
+ }
7
+
8
+ .posts__edit-categories {
9
+ .inputs-checkbox {
10
+ margin: 0;
11
+ margin-bottom: 2px;
12
+ }
13
+
14
+ .elements-table__row {
15
+ td:first-child {
16
+ width: 50px;
17
+ }
18
+ }
19
+ }
20
+
21
+ .posts__index {
22
+ .elements-table__row td:first-child {
23
+ width: 60%;
24
+ }
25
+ }
@@ -0,0 +1,12 @@
1
+ <%
2
+ colors = core__get_colors
3
+ %>
4
+
5
+ $color_white: <%= colors[:white] %>;
6
+ $color_black: <%= colors[:black] %>;
7
+ $color_grey: <%= colors[:grey] %>;
8
+ $color_main: <%= colors[:main] %>;
9
+ $color_active: <%= colors[:active] %>;
10
+ $color_danger: <%= colors[:danger] %>;
11
+ $color_warning: <%= colors[:warning] %>;
12
+ $color_success: <%= colors[:success] %>;
@@ -0,0 +1,3 @@
1
+ .widgets-systeminfo {
2
+
3
+ }
@@ -0,0 +1 @@
1
+ @import "switchlang";
@@ -0,0 +1,5 @@
1
+ module LatoBlog
2
+ class Api::ApiController < ApplicationController
3
+
4
+ end
5
+ end
@@ -0,0 +1,51 @@
1
+ module LatoBlog
2
+ class Api::CategoriesController < Api::ApiController
3
+
4
+ def index
5
+ categories = LatoBlog::Category.all
6
+
7
+ # order categories
8
+ order = (params[:order] && params[:order] == 'ASC') ? 'ASC' : 'DESC'
9
+ categories = categories.order("title #{order}")
10
+
11
+ # filter language
12
+ categories = categories.where(meta_language: params[:language]) if params[:language]
13
+ # filter search
14
+ categories = categories.where('title like ?', "%#{params[:search]}%") if params[:search]
15
+
16
+ # save total categories
17
+ total = categories.length
18
+
19
+ # manage pagination
20
+ page = params[:page] ? params[:page].to_i : 1
21
+ per_page = params[:per_page] ? params[:per_page].to_i : 20
22
+ categories = core__paginate_array(categories, per_page, page)
23
+
24
+ # render response
25
+ core__send_request_success(
26
+ categories: (categories && !categories.empty?) ? categories.map(&:serialize) : [],
27
+ page: page,
28
+ per_page: per_page,
29
+ order: order,
30
+ total: total
31
+ )
32
+ end
33
+
34
+ def show
35
+ # check parameters
36
+ core__send_request_fail('Uncorrect parameters') && return unless params[:id] || params[:permalink]
37
+
38
+ # find post
39
+ if params[:id]
40
+ category = LatoBlog::Category.find_by(id: params[:id].to_i)
41
+ else
42
+ category = LatoBlog::Category.find_by(meta_permalink: params[:permalink])
43
+ end
44
+
45
+ # render respnse
46
+ core__send_request_fail('Category not found') && return unless category
47
+ core__send_request_success(category: category.serialize)
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,93 @@
1
+ module LatoBlog
2
+ class Api::PostsController < Api::ApiController
3
+
4
+ def index
5
+ posts = LatoBlog::Post.published.joins(:categories).joins(:post_parent)
6
+ posts = posts.where('lato_blog_post_parents.publication_datetime <= ?', DateTime.now)
7
+
8
+ # order posts
9
+ order = (params[:order] && params[:order] == 'ASC') ? 'ASC' : 'DESC'
10
+ posts = posts.order("lato_blog_post_parents.publication_datetime #{order}")
11
+
12
+ # filter language
13
+ posts = posts.where(meta_language: params[:language]) if params[:language]
14
+ # filter category permalink
15
+ posts = filter_category_permalink(posts) if params[:category_permalink]
16
+ # filter category id
17
+ posts = filter_category_id(posts) if params[:category_id]
18
+ # filter search
19
+ posts = posts.where('lato_blog_posts.title like ?', "%#{params[:search]}%") if params[:search]
20
+
21
+ # take posts uniqueness
22
+ posts = posts.uniq(&:id)
23
+
24
+ # save total posts
25
+ total = posts.length
26
+
27
+ # manage pagination
28
+ page = params[:page] ? params[:page].to_i : 1
29
+ per_page = params[:per_page] ? params[:per_page].to_i : 20
30
+ posts = core__paginate_array(posts, per_page, page)
31
+
32
+ # render response
33
+ core__send_request_success(
34
+ posts: (posts && !posts.empty?) ? posts.map(&:serialize) : [],
35
+ page: page,
36
+ per_page: per_page,
37
+ order: order,
38
+ total: total
39
+ )
40
+ end
41
+
42
+ def show
43
+ # check parameters
44
+ core__send_request_fail('Uncorrect parameters') && return unless params[:id] || params[:permalink]
45
+
46
+ # find post
47
+ if params[:id]
48
+ post = LatoBlog::Post.find_by(id: params[:id].to_i, meta_status: 'published')
49
+ else
50
+ post = LatoBlog::Post.find_by(meta_permalink: params[:permalink], meta_status: 'published')
51
+ end
52
+
53
+ # render respnse
54
+ core__send_request_fail('Post not found') && return unless post
55
+ core__send_request_success(post: post.serialize)
56
+ end
57
+
58
+ private
59
+
60
+ def filter_category_permalink(posts)
61
+ return posts unless params[:category_permalink]
62
+ category_permalinks = params[:category_permalink].is_a?(Array) ? params[:category_permalink] : params[:category_permalink].split(',')
63
+ posts = posts.where(lato_blog_categories: { meta_permalink: category_permalinks })
64
+ # manage AND clause
65
+ if params[:category_permalink_AND] == 'true'
66
+ ids = []
67
+ posts.pluck(:id).each do |id|
68
+ ids.push(id) if posts.where(id: id).length >= category_permalinks.length
69
+ end
70
+ posts = posts.where(id: ids)
71
+ end
72
+ # return posts
73
+ posts
74
+ end
75
+
76
+ def filter_category_id(posts)
77
+ return posts unless params[:category_id]
78
+ category_ids = params[:category_id].is_a?(Array) ? params[:category_id] : params[:category_id].split(',')
79
+ posts = posts.where(lato_blog_categories: { id: category_ids })
80
+ # manage AND clause
81
+ if params[:category_id_AND] == 'true'
82
+ ids = []
83
+ posts.pluck(:id).each do |id|
84
+ ids.push(id) if posts.where(id: id).length >= category_ids.length
85
+ end
86
+ posts = posts.where(id: ids)
87
+ end
88
+ # return posts
89
+ posts
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,13 @@
1
+ module LatoBlog
2
+
3
+ # ApplicationController
4
+ class ApplicationController < ActionController::Base
5
+
6
+ protect_from_forgery with: :exception
7
+
8
+ def index
9
+ redirect_to lato_blog.posts_path
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module LatoBlog
2
+ class Back::BackController < ApplicationController
3
+
4
+ # Specific helpers import to fix not found functions bug
5
+ helper LatoBlog::ApplicationHelper
6
+ helper LatoBlog::FieldsHelper
7
+
8
+ layout 'lato_core/admin'
9
+
10
+ # check user is logged
11
+ before_action :core__manage_superuser_session
12
+ # set default language if not exist
13
+ before_action :set_default_current_language
14
+
15
+ # This function switch the default language used by the system and redirect to the same user page.
16
+ def switch_current_language
17
+ set_current_language params[:language]
18
+
19
+ respond_to do |format|
20
+ format.js
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # Language helpers:
27
+
28
+ # This function set a default language on cookie if no languages are set.
29
+ def set_default_current_language
30
+ if !cookies[:lato_blog__current_language]
31
+ cookies[:lato_blog__current_language] = BLOG_LANGUAGES_IDENTIFIER.first
32
+ end
33
+ end
34
+
35
+ # This function set the language received as params as default language if it is possible.
36
+ def set_current_language language
37
+ if BLOG_LANGUAGES_IDENTIFIER.include? language
38
+ cookies[:lato_blog__current_language] = language
39
+ else
40
+ set_default_current_language
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,141 @@
1
+ module LatoBlog
2
+ class Back::CategoriesController < Back::BackController
3
+
4
+ before_action do
5
+ core__set_menu_active_item('blog_articles')
6
+ end
7
+
8
+ # This function shows the list of possible categories.
9
+ def index
10
+ core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories])
11
+ # find categories to show
12
+ @categories = LatoBlog::Category.where(meta_language: cookies[:lato_blog__current_language]).order('title ASC')
13
+ @widget_index_categories = core__widgets_index(@categories, search: 'title', pagination: 10)
14
+ end
15
+
16
+ # This function shows a single category. It create a redirect to the edit path.
17
+ def show
18
+ # use edit as default post show page
19
+ redirect_to lato_blog.edit_category_path(params[:id])
20
+ end
21
+
22
+ # This function shows the view to create a new category.
23
+ def new
24
+ core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories_new])
25
+ @category = LatoBlog::Category.new
26
+
27
+ if params[:language]
28
+ set_current_language params[:language]
29
+ end
30
+
31
+ if params[:parent]
32
+ @category_parent = LatoBlog::CategoryParent.find_by(id: params[:parent])
33
+ end
34
+
35
+ fetch_external_objects
36
+ end
37
+
38
+ # This function creates a new category.
39
+ def create
40
+ @category = LatoBlog::Category.new(new_category_params)
41
+
42
+ if !@category.save
43
+ flash[:danger] = @category.errors.full_messages.to_sentence
44
+ redirect_to lato_blog.new_category_path
45
+ return
46
+ end
47
+
48
+ flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_create_success]
49
+ redirect_to lato_blog.category_path(@category.id)
50
+ end
51
+
52
+ # This function show the view to edit a category.
53
+ def edit
54
+ core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories_edit])
55
+ @category = LatoBlog::Category.find_by(id: params[:id])
56
+ return unless check_category_presence
57
+
58
+ if @category.meta_language != cookies[:lato_blog__current_language]
59
+ set_current_language @category.meta_language
60
+ end
61
+
62
+ fetch_external_objects
63
+ end
64
+
65
+ # This function updates a category.
66
+ def update
67
+ @category = LatoBlog::Category.find_by(id: params[:id])
68
+ return unless check_category_presence
69
+
70
+ if !@category.update(edit_category_params)
71
+ flash[:danger] = @category.errors.full_messages.to_sentence
72
+ redirect_to lato_blog.edit_category_path(@category.id)
73
+ return
74
+ end
75
+
76
+ flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_update_success]
77
+ redirect_to lato_blog.category_path(@category.id)
78
+ end
79
+
80
+ # This function destroyes a category.
81
+ def destroy
82
+ @category = LatoBlog::Category.find_by(id: params[:id])
83
+ return unless check_category_presence
84
+
85
+ if !@category.destroy
86
+ flash[:danger] = @category.category_parent.errors.full_messages.to_sentence
87
+ redirect_to lato_blog.edit_category_path(@category.id)
88
+ return
89
+ end
90
+
91
+ flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_destroy_success]
92
+ redirect_to lato_blog.categories_path(status: 'deleted')
93
+ end
94
+
95
+ private
96
+
97
+ def fetch_external_objects
98
+ @categories_list = LatoBlog::Category.where(meta_language: cookies[:lato_blog__current_language]).where.not(
99
+ id: @category.id).map { |cat| { title: cat.title, value: cat.id } }
100
+ end
101
+
102
+ # This function checks the @post variable is present and redirect to index if it not exist.
103
+ def check_category_presence
104
+ if !@category
105
+ flash[:warning] = LANGUAGES[:lato_blog][:flashes][:category_not_found]
106
+ redirect_to lato_blog.categories_path
107
+ return false
108
+ end
109
+
110
+ return true
111
+ end
112
+
113
+ # Params helpers:
114
+
115
+ # This function generate params for a new category.
116
+ def new_category_params
117
+ # take params from front-end request
118
+ category_params = params.require(:category).permit(:title, :lato_blog_category_id).to_h
119
+ # add current superuser id
120
+ category_params[:lato_core_superuser_creator_id] = @core__current_superuser.id
121
+ # add post parent id
122
+ category_params[:lato_blog_category_parent_id] = (params[:parent] && !params[:parent].blank? ? params[:parent] : generate_category_parent)
123
+ # add metadata
124
+ category_params[:meta_language] = cookies[:lato_blog__current_language]
125
+ # return final post object
126
+ return category_params
127
+ end
128
+
129
+ # This function generate params for a edit category.
130
+ def edit_category_params
131
+ params.require(:category).permit(:title, :lato_blog_category_id, :meta_permalink)
132
+ end
133
+
134
+ # This function generate and save a new category parent and return the id.
135
+ def generate_category_parent
136
+ category_parent = LatoBlog::CategoryParent.create
137
+ return category_parent.id
138
+ end
139
+
140
+ end
141
+ end
@@ -0,0 +1,86 @@
1
+ module LatoBlog
2
+ class Back::PostFieldsController < Back::BackController
3
+
4
+ before_action do
5
+ core__set_menu_active_item('blog_articles')
6
+ end
7
+
8
+ def index
9
+ core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:post_fields])
10
+ @post_fields = LatoBlog::PostField.all.order('meta_visible')
11
+ @widget_index_post_fields = core__widgets_index(@post_fields, search: 'key', pagination: 10)
12
+ end
13
+
14
+ def destroy
15
+ @post_field = LatoBlog::PostField.find_by(id: params[:id])
16
+
17
+ unless @post_field
18
+ flash[:warning] = LANGUAGES[:lato_blog][:flashes][:post_field_not_found]
19
+ redirect_to lato_blog.root_path
20
+ return
21
+ end
22
+
23
+ unless @post_field.destroy
24
+ flash[:danger] = @post_field.category_parent.errors.full_messages.to_sentence
25
+ redirect_to lato_blog.post_fields_path
26
+ return
27
+ end
28
+
29
+ flash[:success] = LANGUAGES[:lato_blog][:flashes][:post_field_destroy_success]
30
+ redirect_to lato_blog.post_fields_path
31
+ end
32
+
33
+ # Single fields endpoints:
34
+ # **************************************************************************
35
+
36
+ # This function create a new field for the post.
37
+ def create_relay_field
38
+ # find post field
39
+ @post_field = LatoBlog::PostField.find_by(id: params[:post_field_id])
40
+ unless @post_field
41
+ @error = true
42
+ respond_to { |r| r.js }
43
+ end
44
+ # find subfield info
45
+ child_field_info = nil
46
+ @post_field.meta_datas[:fields].each do |key, content|
47
+ child_field_info = [key, content] if key == params[:key]
48
+ end
49
+ unless child_field_info
50
+ @error = true
51
+ respond_to { |r| r.js }
52
+ end
53
+ # prepare data to create field
54
+ child_field_key = child_field_info.first
55
+ child_field_content = child_field_info.last
56
+ # override class and position
57
+ child_field_content[:class] = nil
58
+ child_field_content[:position] = !@post_field.post_fields.empty? ? @post_field.post_fields.order('position ASC').last.position + 1 : 1
59
+ # create subfield for the post field
60
+ blog__create_db_post_field(@post_field.post, child_field_key, child_field_content, @post_field)
61
+ # send response to client
62
+ @error = false
63
+ respond_to { |r| r.js }
64
+ end
65
+
66
+ # This function destroy a post for the field.
67
+ def destroy_relay_field
68
+ # find post field
69
+ child_field = LatoBlog::PostField.find_by(id: params[:id])
70
+ @post_field = child_field.post_field
71
+ unless @post_field
72
+ @error = true
73
+ respond_to { |r| r.js }
74
+ end
75
+ # find post field child and destroy it
76
+ unless child_field.destroy
77
+ @error = true
78
+ respond_to { |r| r.js }
79
+ end
80
+ # send response to client
81
+ @error = false
82
+ respond_to { |r| r.js }
83
+ end
84
+
85
+ end
86
+ end