lato_blog 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +5 -5
  2. data/MIT-LICENSE +1 -1
  3. data/app/assets/javascripts/lato_blog/modules/BlogPosts.js +8 -0
  4. data/app/assets/stylesheets/lato_blog/pages/_posts.scss +1 -1
  5. data/app/controllers/lato_blog/api/categories_controller.rb +2 -2
  6. data/app/controllers/lato_blog/api/posts_controller.rb +4 -0
  7. data/app/controllers/lato_blog/api/tags_controller.rb +32 -0
  8. data/app/controllers/lato_blog/back/categories_controller.rb +1 -1
  9. data/app/controllers/lato_blog/back/posts_controller.rb +25 -0
  10. data/app/controllers/lato_blog/back/tags_controller.rb +137 -0
  11. data/app/models/lato_blog/post/serializer_helpers.rb +13 -1
  12. data/app/models/lato_blog/post.rb +5 -0
  13. data/app/models/lato_blog/tag/entity_helpers.rb +37 -0
  14. data/app/models/lato_blog/tag/serializer_helpers.rb +52 -0
  15. data/app/models/lato_blog/tag.rb +83 -0
  16. data/app/models/lato_blog/tag_parent.rb +9 -0
  17. data/app/models/lato_blog/tag_post.rb +31 -0
  18. data/app/views/lato_blog/back/categories/edit.html.erb +2 -0
  19. data/app/views/lato_blog/back/categories/index.html.erb +9 -3
  20. data/app/views/lato_blog/back/post_fields/create_relay_field.js.erb +1 -0
  21. data/app/views/lato_blog/back/post_fields/destroy_relay_field.js.erb +1 -0
  22. data/app/views/lato_blog/back/posts/edit.html.erb +4 -0
  23. data/app/views/lato_blog/back/posts/new.html.erb +2 -0
  24. data/app/views/lato_blog/back/posts/shared/edit/_edit_tags.html.erb +28 -0
  25. data/app/views/lato_blog/back/posts/shared/index/_index.html.erb +5 -5
  26. data/app/views/lato_blog/back/tags/edit.html.erb +44 -0
  27. data/app/views/lato_blog/back/tags/index.html.erb +24 -0
  28. data/app/views/lato_blog/back/tags/new.html.erb +40 -0
  29. data/app/views/lato_blog/back/tags/shared/_form.html.erb +34 -0
  30. data/app/views/lato_blog/back/tags/shared/edit/_edit_informations.html.erb +24 -0
  31. data/app/views/lato_blog/back/tags/shared/edit/_edit_languages.html.erb +38 -0
  32. data/app/views/lato_blog/back/tags/shared/new/_new_informations.html.erb +21 -0
  33. data/app/views/lato_blog/doc/doc/index.html.erb +1 -1
  34. data/config/configs.yml +5 -0
  35. data/config/languages/default.yml +11 -1
  36. data/config/languages/it.yml +10 -0
  37. data/config/routes/api.rb +4 -0
  38. data/config/routes/back.rb +5 -1
  39. data/db/migrate/20171020131140_create_lato_blog_tags.rb +15 -0
  40. data/db/migrate/20171021131140_create_lato_blog_tag_parents.rb +8 -0
  41. data/db/migrate/20171022131140_create_lato_blog_tag_posts.rb +10 -0
  42. data/lib/lato_blog/interface.rb +3 -3
  43. data/lib/lato_blog/interfaces/categories.rb +66 -0
  44. data/lib/lato_blog/interfaces/posts.rb +148 -0
  45. data/lib/lato_blog/interfaces/tags.rb +80 -0
  46. data/lib/lato_blog/version.rb +1 -1
  47. metadata +24 -6
  48. data/lib/lato_blog/interfaces/queries.rb +0 -161
@@ -5,14 +5,14 @@
5
5
  <%=raw block.open %>
6
6
 
7
7
  <%
8
- title = "#{get_current_language_title.upcase}: "
9
- title = "#{title}#{LANGUAGES[:lato_blog][:mixed][:published_posts]}" if status && status === 'published'
10
- title = "#{title}#{LANGUAGES[:lato_blog][:mixed][:drafted_posts]}" if status && status === 'drafted'
11
- title = "#{title}#{LANGUAGES[:lato_blog][:mixed][:deleted_posts]}" if status && status === 'deleted'
8
+ title = "#{LANGUAGES[:lato_blog][:mixed][:published_posts]}" if status && status === 'published'
9
+ title = "#{LANGUAGES[:lato_blog][:mixed][:drafted_posts]}" if status && status === 'drafted'
10
+ title = "#{LANGUAGES[:lato_blog][:mixed][:deleted_posts]}" if status && status === 'deleted'
12
11
  %>
13
12
 
14
- <%=raw cell(:elements, :title).new(label: title, size: 6) %>
13
+ <%=raw cell(:elements, :title).new(label: title, size: 4) %>
15
14
 
15
+ <br>
16
16
 
17
17
  <%=raw cell(:widgets, :index).new(
18
18
  records: @widget_index_posts,
@@ -0,0 +1,44 @@
1
+ <%
2
+ row = cell(:elements, :row).new
3
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
4
+ container_block_big = cell(:elements, :block).new(class: 'xs-12 sm-12 md-8 no-padding no-background no-shadow')
5
+ container_block_small = cell(:elements, :block).new(class: 'xs-12 sm-12 md-4 no-padding no-background no-shadow')
6
+ %>
7
+
8
+ <!-- MAIN CONTAINER -->
9
+ <%=raw row.open %>
10
+
11
+ <!-- BIG SECTION CONTAINER -->
12
+ <%=raw container_block_big.open %>
13
+
14
+ <%=raw row.open %>
15
+
16
+ <%=raw block.open %>
17
+
18
+ <%=raw cell(:elements, :title).new(label: @tag.title, size: 4) %>
19
+
20
+ <%= render 'lato_blog/back/tags/shared/form', url: lato_blog.tag_path(@tag.id), method: :put %>
21
+
22
+ <%=raw block.close %>
23
+
24
+ <%=raw row.close %>
25
+
26
+ <%=raw container_block_big.close %>
27
+ <!-- / BIG SECTION CONTAINER -->
28
+
29
+ <!-- SMALL SECTION CONTAINER -->
30
+ <%=raw container_block_small.open %>
31
+
32
+ <%=raw row.open %>
33
+
34
+ <%= render 'lato_blog/back/tags/shared/edit/edit_informations' %>
35
+
36
+ <%= render 'lato_blog/back/tags/shared/edit/edit_languages' %>
37
+
38
+ <%=raw row.close %>
39
+
40
+ <%=raw container_block_small.close %>
41
+ <!-- / SMALL SECTION CONTAINER -->
42
+
43
+ <%=raw row.close %>
44
+ <!-- / MAIN CONTAINER -->
@@ -0,0 +1,24 @@
1
+ <%
2
+ row = cell(:elements, :row).new
3
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
4
+ %>
5
+
6
+ <!-- MAIN CONTAINER -->
7
+ <%=raw row.open %>
8
+
9
+ <%=raw block.open %>
10
+
11
+ <%=raw cell(:widgets, :index).new(
12
+ records: @widget_index_tags,
13
+ index_url: lato_blog.tags_path,
14
+ head: [LANGUAGES[:lato_blog][:forms][:title], LANGUAGES[:lato_blog][:forms][:language], LANGUAGES[:lato_blog][:mixed][:number_of_posts]],
15
+ columns: ['title', 'get_pretty_language', 'get_number_of_posts'],
16
+ actions: {show: true, new: true, delete: true},
17
+ search: true,
18
+ pagination: true
19
+ ) %>
20
+
21
+ <%=raw block.close %>
22
+
23
+ <%=raw row.close %>
24
+ <!-- / MAIN CONTAINER -->
@@ -0,0 +1,40 @@
1
+ <%
2
+ row = cell(:elements, :row).new
3
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
4
+ container_block_big = cell(:elements, :block).new(class: 'xs-12 sm-12 md-8 no-padding no-background no-shadow')
5
+ container_block_small = cell(:elements, :block).new(class: 'xs-12 sm-12 md-4 no-padding no-background no-shadow')
6
+ %>
7
+
8
+ <!-- MAIN CONTAINER -->
9
+ <%=raw row.open %>
10
+
11
+ <!-- BIG SECTION CONTAINER -->
12
+ <%=raw container_block_big.open %>
13
+
14
+ <%=raw row.open %>
15
+
16
+ <%=raw block.open %>
17
+
18
+ <%= render 'lato_blog/back/tags/shared/form', url: lato_blog.tags_path, method: :post %>
19
+
20
+ <%=raw block.close %>
21
+
22
+ <%=raw row.close %>
23
+
24
+ <%=raw container_block_big.close %>
25
+ <!-- / BIG SECTION CONTAINER -->
26
+
27
+ <!-- SMALL SECTION CONTAINER -->
28
+ <%=raw container_block_small.open %>
29
+
30
+ <%=raw row.open %>
31
+
32
+ <%= render 'lato_blog/back/tags/shared/new/new_informations' %>
33
+
34
+ <%=raw row.close %>
35
+
36
+ <%=raw container_block_small.close %>
37
+ <!-- / SMALL SECTION CONTAINER -->
38
+
39
+ <%=raw row.close %>
40
+ <!-- / MAIN CONTAINER -->
@@ -0,0 +1,34 @@
1
+ <%
2
+ row = cell(:elements, :row).new
3
+ form = cell(:inputs, :form).new(url: url, method: method)
4
+ %>
5
+
6
+ <%=raw form.open %>
7
+
8
+ <input type="hidden" name="parent" value="<%= @tag_parent ? @tag_parent.id : nil %>">
9
+
10
+ <%=raw row.open %>
11
+
12
+ <% if method === :put %>
13
+
14
+ <%=raw cell(:inputs, :text).new(name: 'tag[meta_permalink]', label: LANGUAGES[:lato_blog][:forms][:permalink],
15
+ required: true, class: 'md-12', value: @tag.meta_permalink) %>
16
+
17
+ <% end %>
18
+
19
+ <%=raw cell(:inputs, :text).new(name: 'tag[title]', label: LANGUAGES[:lato_blog][:forms][:title],
20
+ required: true, class: 'md-12', value: @tag.title) %>
21
+
22
+ <%=raw row.close %>
23
+
24
+ <div class="text-right">
25
+
26
+ <%=raw cell(:elements, :button).new(type: 'link', label: LANGUAGES[:lato_blog][:mixed][:cancel],
27
+ style: 'danger', icon: 'ban', url: lato_blog.tags_path) %>
28
+
29
+ <%=raw cell(:elements, :button).new(type: 'submit', label: LANGUAGES[:lato_blog][:mixed][:save],
30
+ style: 'success', icon: 'check') %>
31
+
32
+ </div>
33
+
34
+ <%=raw form.close %>
@@ -0,0 +1,24 @@
1
+ <%
2
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
3
+ table = cell(:elements, :table, :container).new(border: true)
4
+ table_body = cell(:elements, :table, :body).new
5
+ %>
6
+
7
+ <%=raw block.open %>
8
+
9
+ <%=raw cell(:elements, :title).new(label: LANGUAGES[:lato_blog][:mixed][:info], size: 6) %>
10
+
11
+ <%=raw table.open %>
12
+
13
+ <%=raw table_body.open %>
14
+
15
+ <%=raw cell(:elements, :table, :row).new(labels: [LANGUAGES[:lato_blog][:mixed][:current_language], get_current_language_title]) %>
16
+ <%=raw cell(:elements, :table, :row).new(labels: [LANGUAGES[:lato_blog][:mixed][:creation_date], @tag.get_pretty_created_at]) %>
17
+ <%=raw cell(:elements, :table, :row).new(labels: [LANGUAGES[:lato_blog][:mixed][:update_date], @tag.get_pretty_updated_at]) %>
18
+ <%=raw cell(:elements, :table, :row).new(labels: [LANGUAGES[:lato_blog][:mixed][:superuser_creator], @tag.get_superuser_creator_name]) %>
19
+
20
+ <%=raw table_body.close %>
21
+
22
+ <%=raw table.close %>
23
+
24
+ <%=raw block.close %>
@@ -0,0 +1,38 @@
1
+ <%
2
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
3
+ table = cell(:elements, :table, :container).new
4
+ table_body = cell(:elements, :table, :body).new
5
+ %>
6
+
7
+ <%=raw block.open %>
8
+
9
+ <%=raw cell(:elements, :title).new(label: LANGUAGES[:lato_blog][:mixed][:languages], size: 6) %>
10
+
11
+ <%=raw table.open %>
12
+
13
+ <%=raw table_body.open %>
14
+
15
+ <% CONFIGS[:lato_blog][:languages].values.each do |language| %>
16
+
17
+ <% if @tag.meta_language === language[:identifier] %>
18
+ <%=raw cell(:elements, :table, :row).new(labels: [language[:title], '']) %>
19
+ <% elsif @tag.get_translation_for_language(language[:identifier]) %>
20
+ <% language_tag = @tag.get_translation_for_language(language[:identifier]) %>
21
+ <%=raw cell(:elements, :table, :row).new(labels: [
22
+ language[:title],
23
+ "<a href='#{lato_blog.tag_path(language_tag.id)}'>#{LANGUAGES[:lato_blog][:mixed][:show]}</a>"
24
+ ]) %>
25
+ <% else %>
26
+ <%=raw cell(:elements, :table, :row).new(labels: [
27
+ language[:title],
28
+ "<a href='#{lato_blog.new_tag_path(language: language[:identifier], parent: @tag.tag_parent.id)}'>#{LANGUAGES[:lato_blog][:mixed][:create]}</a>"
29
+ ]) %>
30
+ <% end %>
31
+
32
+ <% end %>
33
+
34
+ <%=raw table_body.close %>
35
+
36
+ <%=raw table.close %>
37
+
38
+ <%=raw block.close %>
@@ -0,0 +1,21 @@
1
+ <%
2
+ block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12')
3
+ table = cell(:elements, :table, :container).new(border: true)
4
+ table_body = cell(:elements, :table, :body).new
5
+ %>
6
+
7
+ <%=raw block.open %>
8
+
9
+ <%=raw cell(:elements, :title).new(label: LANGUAGES[:lato_blog][:mixed][:info], size: 6) %>
10
+
11
+ <%=raw table.open %>
12
+
13
+ <%=raw table_body.open %>
14
+
15
+ <%=raw cell(:elements, :table, :row).new(labels: [LANGUAGES[:lato_blog][:mixed][:current_language], get_current_language_title]) %>
16
+
17
+ <%=raw table_body.close %>
18
+
19
+ <%=raw table.close %>
20
+
21
+ <%=raw block.close %>
@@ -66,10 +66,10 @@ post_fields:
66
66
  <%=raw cell(:elements, :title).new(label: 'Base fields', size: 4) %>
67
67
 
68
68
  <%=raw cell(:elements, :button).new(label: 'Text', url: lato_blog.doc_fields_text_path) %>
69
+ <%=raw cell(:elements, :button).new(label: 'Image', url: lato_blog.doc_fields_image_path) %>
69
70
  <%=raw cell(:elements, :button).new(label: 'Datetime', url: lato_blog.doc_fields_datetime_path) %>
70
71
  <%=raw cell(:elements, :button).new(label: 'Editor', url: lato_blog.doc_fields_editor_path) %>
71
72
  <%=raw cell(:elements, :button).new(label: 'Geolocalization', url: lato_blog.doc_fields_geolocalization_path) %>
72
- <%=raw cell(:elements, :button).new(label: 'Image', url: lato_blog.doc_fields_image_path) %>
73
73
 
74
74
  <br> <br>
75
75
  <%=raw cell(:elements, :title).new(label: 'Group fields', size: 4) %>
data/config/configs.yml CHANGED
@@ -79,6 +79,11 @@ menu:
79
79
  url: /lato/blog/categories
80
80
  permission_min: 0
81
81
  permission_max: 99
82
+ tags:
83
+ title: translate[posts_tags]
84
+ url: /lato/blog/tags
85
+ permission_min: 0
86
+ permission_max: 99
82
87
  fields:
83
88
  title: translate[post_fields]
84
89
  url: /lato/blog/post_fields
@@ -3,6 +3,7 @@ menu:
3
3
  posts_new: Add new
4
4
  posts_index: Show all
5
5
  posts_categories: Categories
6
+ posts_tags: Tags
6
7
  post_fields: Fields
7
8
  documentation: Doc. Blog
8
9
 
@@ -14,6 +15,9 @@ pages:
14
15
  categories: Categories
15
16
  categories_new: New category
16
17
  categories_edit: Edit category
18
+ tags: Tags
19
+ tags_new: New tag
20
+ tags_edit: Edit tag
17
21
  documentation: Documentation
18
22
 
19
23
  flashes:
@@ -29,6 +33,10 @@ flashes:
29
33
  category_not_found: Category not found
30
34
  category_update_success: The Category was updated successfully
31
35
  category_destroy_success: The Category was destroied successfully
36
+ tag_create_success: The Tag was created successfully
37
+ tag_not_found: Tag not found
38
+ tag_update_success: The Tag was updated successfully
39
+ tag_destroy_success: The Tag was destroied successfully
32
40
  post_categories_not_accepted: There are not categories selected
33
41
  post_update_fields_warning: The post is updated but we have some problems with fields
34
42
  post_field_not_found: Field not found
@@ -72,7 +80,8 @@ mixed:
72
80
  publication_datetime: Publication date
73
81
  child_tree: Child tree
74
82
  categories: Categories
75
- number_of_posts: Number of posts
83
+ tags: Tags
84
+ number_of_posts: Number of articles
76
85
  key: Key
77
86
  typology: Typology
78
87
  visibility: Visibility
@@ -80,6 +89,7 @@ mixed:
80
89
  post_id: Post id
81
90
  post_field_id: Post field id
82
91
  seo_description: Seo description
92
+ create_new_post: Create a new article
83
93
 
84
94
  posts_status:
85
95
  published: Published
@@ -3,6 +3,7 @@ menu:
3
3
  posts_new: Aggiungi nuovo
4
4
  posts_index: Visualizza tutti
5
5
  posts_categories: Categorie
6
+ post_tags: Tags
6
7
  post_fields: Campi
7
8
  documentation: Doc. Blog
8
9
 
@@ -14,6 +15,9 @@ pages:
14
15
  categories: Categorie
15
16
  categories_new: Nuova categoria
16
17
  categories_edit: Modifica categoria
18
+ tags: Tags
19
+ tags_new: Nuovo tag
20
+ tags_edit: Modifica tag
17
21
  documentation: Documentazione
18
22
 
19
23
  flashes:
@@ -29,6 +33,10 @@ flashes:
29
33
  category_not_found: Categoria non trovata
30
34
  category_update_success: Categoria aggiornata con successo
31
35
  category_destroy_success: Categoria eliminata con successo
36
+ tag_create_success: Tag creato con successo
37
+ tag_not_found: Tag non trovato
38
+ tag_update_success: Tag aggiornato con successo
39
+ tag_destroy_success: Tag eliminato con successo
32
40
  post_categories_not_accepted: Non ci sono categorie selezionate
33
41
  post_update_fields_warning: L'articolo è stato aggiornato ma ci sono state problematiche con il salvataggio dei campi
34
42
  post_field_not_found: Campo non trovato
@@ -72,6 +80,7 @@ mixed:
72
80
  publication_datetime: Data di pubblicazione
73
81
  child_tree: Albero dei figli
74
82
  categories: Categorie
83
+ tags: Tags
75
84
  number_of_posts: Numero di articoli
76
85
  key: Chiave
77
86
  typology: Tipologia
@@ -80,6 +89,7 @@ mixed:
80
89
  post_id: Id articolo
81
90
  post_field_id: Id campo
82
91
  seo_description: Descrizione seo
92
+ create_new_post: Crea un nuovo articolo
83
93
 
84
94
  posts_status:
85
95
  published: Pubblico
data/config/routes/api.rb CHANGED
@@ -8,4 +8,8 @@ namespace :api do
8
8
  get 'categories', to: 'categories#index', as: 'categories_index'
9
9
  get 'category', to: 'categories#show', as: 'categories_show'
10
10
 
11
+ # tags
12
+ get 'tags', to: 'tags#index', as: 'tags_index'
13
+ get 'tag', to: 'tags#show', as: 'tags_show'
14
+
11
15
  end
@@ -6,6 +6,7 @@ resources :posts, module: 'back'
6
6
  post 'posts_extra/update_status', to: 'back/posts#update_status', as: 'posts_update_status'
7
7
  post 'posts_extra/update_publication_datetime', to: 'back/posts#update_publication_datetime', as: 'posts_update_publication_datetime'
8
8
  post 'posts_extra/update_categories', to: 'back/posts#update_categories', as: 'posts_update_categories'
9
+ post 'posts_extra/update_tags', to: 'back/posts#update_tags', as: 'posts_update_tags'
9
10
  post 'posts_extra/update_seo_description', to: 'back/posts#update_seo_description', as: 'posts_update_seo_description'
10
11
  get 'posts_extra/destroy_all_deleted', to: 'back/posts#destroy_all_deleted', as: 'posts_destroy_all_deleted'
11
12
 
@@ -15,4 +16,7 @@ post 'post_fields_extra/create_relay_field', to: 'back/post_fields#create_relay_
15
16
  post 'post_fields_extra/destroy_relay_field', to: 'back/post_fields#destroy_relay_field', as: 'post_fields_destroy_relay_field'
16
17
 
17
18
  # categories
18
- resources :categories, module: 'back'
19
+ resources :categories, module: 'back'
20
+
21
+ # tags
22
+ resources :tags, module: 'back'
@@ -0,0 +1,15 @@
1
+ class CreateLatoBlogTags < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :lato_blog_tags do |t|
4
+ t.string :title
5
+
6
+ t.string :meta_permalink
7
+ t.string :meta_language
8
+
9
+ t.integer :lato_core_superuser_creator_id
10
+ t.integer :lato_blog_tag_parent_id
11
+
12
+ t.timestamps
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ class CreateLatoBlogTagParents < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :lato_blog_tag_parents do |t|
4
+ # TODO
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class CreateLatoBlogTagPosts < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :lato_blog_tag_posts do |t|
4
+ t.integer :lato_blog_tag_id
5
+ t.integer :lato_blog_post_id
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -10,11 +10,11 @@ module LatoBlog
10
10
  require 'lato_blog/interfaces/categories'
11
11
  include LatoBlog::Interface::Categories
12
12
 
13
+ require 'lato_blog/interfaces/tags'
14
+ include LatoBlog::Interface::Tags
15
+
13
16
  require 'lato_blog/interfaces/fields'
14
17
  include LatoBlog::Interface::Fields
15
18
 
16
- require 'lato_blog/interfaces/queries'
17
- include LatoBlog::Interface::Queries
18
-
19
19
  end
20
20
  end
@@ -30,5 +30,71 @@ module LatoBlog
30
30
  category_parents.map { |cp| cp.destroy if cp.categories.empty? }
31
31
  end
32
32
 
33
+ # This function returns an object with the list of categories with some filters.
34
+ def blog__get_categories(
35
+ order: nil,
36
+ language: nil,
37
+ search: nil,
38
+ page: nil,
39
+ per_page: nil
40
+ )
41
+ categories = LatoBlog::Category.all
42
+
43
+ # apply filters
44
+ order = order && order == 'ASC' ? 'ASC' : 'DESC'
45
+ categories = _categories_filter_by_order(categories, order)
46
+ categories = _categories_filter_by_language(categories, language)
47
+ categories = _categories_filter_search(categories, search)
48
+
49
+ # take categories uniqueness
50
+ categories = categories.uniq(&:id)
51
+
52
+ # save total categories
53
+ total = categories.length
54
+
55
+ # manage pagination
56
+ page = page&.to_i || 1
57
+ per_page = per_page&.to_i || 20
58
+ categories = core__paginate_array(categories, per_page, page)
59
+
60
+ # return result
61
+ {
62
+ categories: categories && !categories.empty? ? categories.map(&:serialize) : [],
63
+ page: page,
64
+ per_page: per_page,
65
+ order: order,
66
+ total: total
67
+ }
68
+ end
69
+
70
+ # This function returns a single category searched by id or permalink.
71
+ def blog__get_category(id: nil, permalink: nil)
72
+ return {} unless id || permalink
73
+
74
+ if id
75
+ category = LatoBlog::Category.find_by(id: id.to_i)
76
+ else
77
+ category = LatoBlog::Category.find_by(meta_permalink: permalink)
78
+ end
79
+
80
+ category.serialize
81
+ end
82
+
83
+ private
84
+
85
+ def _categories_filter_by_order(categories, order)
86
+ categories.order("title #{order}")
87
+ end
88
+
89
+ def _categories_filter_by_language(categories, language)
90
+ return categories unless language
91
+ categories.where(meta_language: language)
92
+ end
93
+
94
+ def _categories_filter_search(categories, search)
95
+ return categories unless search
96
+ categories.where('title like ?', "%#{search}%")
97
+ end
98
+
33
99
  end
34
100
  end
@@ -9,5 +9,153 @@ module LatoBlog
9
9
  post_parents.map { |pp| pp.destroy if pp.posts.empty? }
10
10
  end
11
11
 
12
+ # This function returns an object with the list of posts with some filters.
13
+ def blog__get_posts(
14
+ order: nil,
15
+ language: nil,
16
+ category_permalink: nil,
17
+ category_permalink_AND: false,
18
+ category_id: nil,
19
+ category_id_AND: false,
20
+ tag_permalink: nil,
21
+ tag_permalink_AND: false,
22
+ tag_id: nil,
23
+ tag_id_AND: false,
24
+ search: nil,
25
+ page: nil,
26
+ per_page: nil
27
+ )
28
+ posts = LatoBlog::Post.published.joins(:post_parent).where('lato_blog_post_parents.publication_datetime <= ?', DateTime.now)
29
+
30
+ # apply filters
31
+ order = order && order == 'ASC' ? 'ASC' : 'DESC'
32
+ posts = _posts_filter_by_order(posts, order)
33
+ posts = _posts_filter_by_language(posts, language)
34
+ if category_permalink || category_id
35
+ posts = posts.joins(:categories)
36
+ posts = _posts_filter_by_category_permalink(posts, category_permalink, category_permalink_AND)
37
+ posts = _posts_filter_category_id(posts, category_id, category_id_AND)
38
+ end
39
+ if tag_permalink || tag_id
40
+ posts = posts.joins(:tags)
41
+ posts = _posts_filter_by_tag_permalink(posts, tag_permalink, tag_permalink_AND)
42
+ posts = _posts_filter_tag_id(posts, tag_id, tag_id_AND)
43
+ end
44
+ posts = _posts_filter_search(posts, search)
45
+
46
+ # take posts uniqueness
47
+ posts = posts.uniq(&:id)
48
+
49
+ # save total posts
50
+ total = posts.length
51
+
52
+ # manage pagination
53
+ page = page&.to_i || 1
54
+ per_page = per_page&.to_i || 20
55
+ posts = core__paginate_array(posts, per_page, page)
56
+
57
+ # return result
58
+ {
59
+ posts: posts && !posts.empty? ? posts.map(&:serialize) : [],
60
+ page: page,
61
+ per_page: per_page,
62
+ order: order,
63
+ total: total
64
+ }
65
+ end
66
+
67
+ # This function returns a single post searched by id or permalink.
68
+ def blog__get_post(id: nil, permalink: nil)
69
+ return {} unless id || permalink
70
+
71
+ if id
72
+ post = LatoBlog::Post.find_by(id: id.to_i, meta_status: 'published')
73
+ else
74
+ post = LatoBlog::Post.find_by(meta_permalink: permalink, meta_status: 'published')
75
+ end
76
+
77
+ post.serialize
78
+ end
79
+
80
+ private
81
+
82
+ def _posts_filter_by_order(posts, order)
83
+ posts.order("lato_blog_post_parents.publication_datetime #{order}")
84
+ end
85
+
86
+ def _posts_filter_by_language(posts, language)
87
+ return posts unless language
88
+ posts.where(meta_language: language)
89
+ end
90
+
91
+ def _posts_filter_by_category_permalink(posts, category_permalink, category_permalink_AND)
92
+ return posts unless category_permalink
93
+ category_permalinks = category_permalink.is_a?(Array) ? category_permalink : category_permalink.split(',')
94
+ posts = posts.where(lato_blog_categories: { meta_permalink: category_permalinks })
95
+ # manage AND clause
96
+ if ['true', true].include?(category_permalink_AND)
97
+ ids = []
98
+ posts.pluck(:id).each do |id|
99
+ ids.push(id) if posts.where(id: id).length >= category_permalinks.length
100
+ end
101
+ posts = posts.where(id: ids)
102
+ end
103
+ # return posts
104
+ posts
105
+ end
106
+
107
+ def _posts_filter_category_id(posts, category_id, category_id_AND)
108
+ return posts unless category_id
109
+ category_ids = category_id.is_a?(Array) ? category_id : category_id.split(',')
110
+ posts = posts.where(lato_blog_categories: { id: category_ids })
111
+ # manage AND clause
112
+ if ['true', true].include?(category_id_AND)
113
+ ids = []
114
+ posts.pluck(:id).each do |id|
115
+ ids.push(id) if posts.where(id: id).length >= category_ids.length
116
+ end
117
+ posts = posts.where(id: ids)
118
+ end
119
+ # return posts
120
+ posts
121
+ end
122
+
123
+ def _posts_filter_by_tag_permalink(posts, tag_permalink, tag_permalink_AND)
124
+ return posts unless tag_permalink
125
+ tag_permalinks = tag_permalink.is_a?(Array) ? tag_permalink : tag_permalink.split(',')
126
+ posts = posts.where(lato_blog_tags: { meta_permalink: tag_permalinks })
127
+ # manage AND clause
128
+ if ['true', true].include?(tag_permalink_AND)
129
+ ids = []
130
+ posts.pluck(:id).each do |id|
131
+ ids.push(id) if posts.where(id: id).length >= tag_permalinks.length
132
+ end
133
+ posts = posts.where(id: ids)
134
+ end
135
+ # return posts
136
+ posts
137
+ end
138
+
139
+ def _posts_filter_tag_id(posts, tag_id, tag_id_AND)
140
+ return posts unless tag_id
141
+ tag_ids = tag_id.is_a?(Array) ? tag_id : tag_id.split(',')
142
+ posts = posts.where(lato_blog_tags: { id: tag_ids })
143
+ # manage AND clause
144
+ if ['true', true].include?(tag_id_AND)
145
+ ids = []
146
+ posts.pluck(:id).each do |id|
147
+ ids.push(id) if posts.where(id: id).length >= tag_ids.length
148
+ end
149
+ posts = posts.where(id: ids)
150
+ end
151
+ # return posts
152
+ posts
153
+ end
154
+
155
+ def _posts_filter_search(posts, search)
156
+ return posts unless search
157
+ posts.where('lato_blog_posts.title like ?', "%#{params[:search]}%")
158
+ end
159
+
12
160
  end
13
161
  end