lato_blog 2.1.1 → 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a60707402fba5e85043a6cf3be5bbab59d225a8
4
- data.tar.gz: a2e8fe0d368e2f37e5ae6865e2da1b68842dad13
3
+ metadata.gz: aad2a0a2115db531a078ab05c7694142da5bb307
4
+ data.tar.gz: dafd5bdb23bcdcb082965d2e81125c11c6ad3fc5
5
5
  SHA512:
6
- metadata.gz: 813fe501d514cdc1454b0b0f389c3dd7774f8cd9e30923cca9693d99e2a18d23a38a95b4adb7514562ae6335bd61c09a0b11ad16f574f1bd6b1733d9bc45524c
7
- data.tar.gz: 91f440dfdc24434b462df9add71f9e4e38accd784f4ffea96b3d1b4f2de5e695632963d735cf33da608ed743675b4510349351e058cae81d6a71659018616d62
6
+ metadata.gz: 3b055147664621e25759b0de18a9e293d72fd745086d2f24a0e869a78dfbfa6f2f625f69ca9e544f578e9df469e4c90ad176bb093d36280d15b5d71eb37721ca
7
+ data.tar.gz: 416b158ff15f8cd5e9f29f1dd2d6bc5508f999fca232edc25fbc1a91365d4751c222eba70a3fcaace76710a244fb64a5040e01373424823721f668e930fd3c0f
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # Lato Blog
2
2
 
3
+ Lato is a Rails engine used to develop modular admin panels. This is the module used to manage a blog.
4
+
3
5
  ## Installation
4
6
 
7
+ Install [lato_core](https://github.com/ideonetwork/lato-core) and [lato_media](https://github.com/ideonetwork/lato-media) gem as required dependencies.
8
+
5
9
  Add the lato_blog gem on your Gemfile
6
10
 
7
11
  ```ruby
@@ -19,6 +19,10 @@
19
19
 
20
20
  .posts__index {
21
21
  .elements-table__row td:first-child {
22
- width: 60%;
22
+ width: 50%;
23
+ }
24
+
25
+ .elements-table__row td:last-child {
26
+ min-width: 100px;
23
27
  }
24
28
  }
@@ -2,50 +2,31 @@ module LatoBlog
2
2
  class Api::CategoriesController < Api::ApiController
3
3
 
4
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)
5
+ result = blog__get_categories(
6
+ order: params[:order],
7
+ language: params[:language],
8
+ search: params[:search],
9
+ page: params[:page],
10
+ per_page: params[:per_page]
11
+ )
23
12
 
24
13
  # 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
- )
14
+ core__send_request_success(result)
32
15
  end
33
16
 
34
17
  def show
35
18
  # check parameters
36
19
  core__send_request_fail('Uncorrect parameters') && return unless params[:id] || params[:permalink]
37
20
 
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
21
+ category = blog__get_category(
22
+ id: params[:id],
23
+ permalink: params[:permalink]
24
+ )
44
25
 
45
26
  # render respnse
46
27
  core__send_request_fail('Category not found') && return unless category
47
- core__send_request_success(category: category.serialize)
28
+ core__send_request_success(category: category)
48
29
  end
49
30
 
50
31
  end
51
- end
32
+ end
@@ -2,91 +2,34 @@ module LatoBlog
2
2
  class Api::PostsController < Api::ApiController
3
3
 
4
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)
5
+ result = blog__get_posts(
6
+ order: params[:order],
7
+ language: params[:language],
8
+ category_permalink: params[:category_permalink],
9
+ category_permalink_AND: params[:category_permalink_AND],
10
+ category_id: params[:category_id],
11
+ category_id_AND: params[:category_id_AND],
12
+ search: params[:search],
13
+ page: params[:page],
14
+ per_page: params[:per_page]
15
+ )
31
16
 
32
17
  # 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
- )
18
+ core__send_request_success(result)
40
19
  end
41
20
 
42
21
  def show
43
22
  # check parameters
44
23
  core__send_request_fail('Uncorrect parameters') && return unless params[:id] || params[:permalink]
45
24
 
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
25
+ post = blog__get_post(
26
+ id: params[:id],
27
+ permalink: params[:permalink]
28
+ )
52
29
 
53
30
  # render respnse
54
31
  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
32
+ core__send_request_success(post: post)
90
33
  end
91
34
 
92
35
  end
@@ -38,5 +38,10 @@ module LatoBlog
38
38
  return self.post_parent.posts.find_by(meta_language: language_identifier)
39
39
  end
40
40
 
41
+ # This function return the list of titles of categories.
42
+ def get_category_titles
43
+ categories.pluck(:title).to_sentence
44
+ end
45
+
41
46
  end
42
47
  end
@@ -53,18 +53,21 @@ module LatoBlog
53
53
 
54
54
  # Datetime.
55
55
  def serialize_field_value_datetime
56
- date = DateTime.parse(value)
57
- serialized = {}
58
-
59
- # add basic info
60
- serialized[:datetime] = date
61
- serialized[:year] = date.year
62
- serialized[:month] = date.month
63
- serialized[:day] = date.day
64
- serialized[:hour] = date.hour
65
- serialized[:minute] = date.min
66
- serialized[:second] = date.sec
67
56
 
57
+ begin
58
+ date = DateTime.parse(value)
59
+ serialized = {}
60
+
61
+ serialized[:datetime] = date
62
+ serialized[:year] = date.year
63
+ serialized[:month] = date.month
64
+ serialized[:day] = date.day
65
+ serialized[:hour] = date.hour
66
+ serialized[:minute] = date.min
67
+ serialized[:second] = date.sec
68
+ rescue StandardError
69
+ serialized = {}
70
+ end
68
71
  # return serialized data
69
72
  serialized
70
73
  end
@@ -10,6 +10,6 @@
10
10
  BlogFields.init()
11
11
  CoreDatepicker.init()
12
12
  CoreEditor.init()
13
- CoreMediapicker.init()
13
+ MediaPicker.init()
14
14
 
15
15
  <% end %>
@@ -10,6 +10,6 @@
10
10
  BlogFields.init()
11
11
  CoreDatepicker.init()
12
12
  CoreEditor.init()
13
- CoreMediapicker.init()
13
+ MediaPicker.init()
14
14
 
15
15
  <% end %>
@@ -1,28 +1,27 @@
1
1
  <%
2
2
  block = cell(:elements, :block).new(class: 'xs-12 sm-12 md-12 posts__index')
3
- table = cell(:elements, :table, :container).new
4
- table_head = cell(:elements, :table, :head).new(labels: [
5
- LANGUAGES[:lato_blog][:forms][:title],
6
- LANGUAGES[:lato_blog][:forms][:language],
7
- LANGUAGES[:lato_blog][:forms][:publication]
8
- ])
9
- table_body = cell(:elements, :table, :body).new
10
3
  %>
11
4
 
12
5
  <%=raw block.open %>
13
6
 
14
7
  <%
15
- title = ''
16
- title = LANGUAGES[:lato_blog][:mixed][:published_posts] if status && status === 'published'
17
- title = LANGUAGES[:lato_blog][:mixed][:drafted_posts] if status && status === 'drafted'
18
- title = LANGUAGES[:lato_blog][:mixed][:deleted_posts] if status && status === 'deleted'
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'
19
12
  %>
20
13
 
21
14
  <%=raw cell(:elements, :title).new(label: title, size: 6) %>
22
15
 
23
16
 
24
- <%=raw cell(:widgets, :index).new(records: @widget_index_posts, index_url: lato_blog.posts_path(status: @posts_status),
25
- head: [LANGUAGES[:lato_blog][:forms][:title], LANGUAGES[:lato_blog][:forms][:language]],
26
- columns: ['title', 'get_pretty_language'], actions: {show: true, new: true, delete: (@posts_status === 'deleted')}, search: true, pagination: true) %>
17
+ <%=raw cell(:widgets, :index).new(
18
+ records: @widget_index_posts,
19
+ index_url: lato_blog.posts_path(status: @posts_status),
20
+ head: [LANGUAGES[:lato_blog][:forms][:title], LANGUAGES[:lato_blog][:mixed][:categories]],
21
+ columns: ['title', 'get_category_titles'],
22
+ actions: {show: true, new: true, delete: (@posts_status === 'deleted')},
23
+ search: true,
24
+ pagination: true
25
+ ) %>
27
26
 
28
27
  <%=raw block.close %>
@@ -1 +0,0 @@
1
- <h1>TODO</h1>
@@ -1 +0,0 @@
1
- <h1>TODO</h1>
@@ -1,9 +1,160 @@
1
1
  module LatoBlog
2
-
3
- # This module contains a list of helpers used on controllers to get posts.
2
+
3
+ # This module contains a list of helpers used on controllers to get posts
4
+ # to render the blog elements.
4
5
  module Interface::Queries
5
6
 
6
- # TODO
7
+ # This function returns an object with the list of posts with some filters.
8
+ def blog__get_posts(order: nil, language: nil, category_permalink: nil, category_permalink_AND: false, category_id: nil, category_id_AND: false, search: nil, page: nil, per_page: nil)
9
+ posts = LatoBlog::Post.published.joins(:categories).joins(:post_parent).where('lato_blog_post_parents.publication_datetime <= ?', DateTime.now)
10
+
11
+ # apply filters
12
+ order = order && order == 'ASC' ? 'ASC' : 'DESC'
13
+ posts = _posts_filter_by_order(posts, order)
14
+ posts = _posts_filter_by_language(posts, language)
15
+ posts = _posts_filter_by_category_permalink(posts, category_permalink, category_permalink_AND)
16
+ posts = _posts_filter_category_id(posts, category_id, category_id_AND)
17
+ posts = _posts_filter_search(posts, search)
18
+
19
+ # take posts uniqueness
20
+ posts = posts.uniq(&:id)
21
+
22
+ # save total posts
23
+ total = posts.length
24
+
25
+ # manage pagination
26
+ page ||= 1
27
+ per_page ||= 20
28
+ posts = core__paginate_array(posts, per_page, page)
29
+
30
+ # return result
31
+ {
32
+ posts: posts && !posts.empty? ? posts.map(&:serialize) : [],
33
+ page: page,
34
+ per_page: per_page,
35
+ order: order,
36
+ total: total
37
+ }
38
+ end
39
+
40
+ # This function returns a single post searched by id or permalink.
41
+ def blog__get_post(id: nil, permalink: nil)
42
+ return {} unless id || permalink
43
+
44
+ if id
45
+ post = LatoBlog::Post.find_by(id: id.to_i, meta_status: 'published')
46
+ else
47
+ post = LatoBlog::Post.find_by(meta_permalink: permalink, meta_status: 'published')
48
+ end
49
+
50
+ post.serialize
51
+ end
52
+
53
+ # This function returns an object with the list of categories with some filters.
54
+ def blog__get_categories(order: nil, language: nil, search: nil, page: nil, per_page: nil)
55
+ categories = LatoBlog::Category.all
56
+
57
+ # apply filters
58
+ order = order && order == 'ASC' ? 'ASC' : 'DESC'
59
+ categories = _categories_filter_by_order(categories, order)
60
+ categories = _categories_filter_by_language(categories, language)
61
+ categories = _categories_filter_search(categories, search)
62
+
63
+ # take categories uniqueness
64
+ categories = categories.uniq(&:id)
65
+
66
+ # save total categories
67
+ total = categories.length
68
+
69
+ # manage pagination
70
+ page ||= 1
71
+ per_page ||= 20
72
+ categories = core__paginate_array(categories, per_page, page)
73
+
74
+ # return result
75
+ {
76
+ categories: categories && !categories.empty? ? categories.map(&:serialize) : [],
77
+ page: page,
78
+ per_page: per_page,
79
+ order: order,
80
+ total: total
81
+ }
82
+ end
83
+
84
+ # This function returns a single category searched by id or permalink.
85
+ def blog__get_category(id: nil, permalink: nil)
86
+ return {} unless id || permalink
87
+
88
+ if id
89
+ category = LatoBlog::Category.find_by(id: id.to_i)
90
+ else
91
+ category = LatoBlog::Category.find_by(meta_permalink: permalink)
92
+ end
93
+
94
+ category.serialize
95
+ end
96
+
97
+ private
98
+
99
+ def _posts_filter_by_order(posts, order)
100
+ posts.order("lato_blog_post_parents.publication_datetime #{order}")
101
+ end
102
+
103
+ def _posts_filter_by_language(posts, language)
104
+ return posts unless language
105
+ posts.where(meta_language: language)
106
+ end
107
+
108
+ def _posts_filter_by_category_permalink(posts, category_permalink, category_permalink_AND)
109
+ return posts unless category_permalink
110
+ category_permalinks = category_permalink.is_a?(Array) ? category_permalink : category_permalink.split(',')
111
+ posts = posts.where(lato_blog_categories: { meta_permalink: category_permalinks })
112
+ # manage AND clause
113
+ if ['true', true].include?(category_permalink_AND)
114
+ ids = []
115
+ posts.pluck(:id).each do |id|
116
+ ids.push(id) if posts.where(id: id).length >= category_permalinks.length
117
+ end
118
+ posts = posts.where(id: ids)
119
+ end
120
+ # return posts
121
+ posts
122
+ end
123
+
124
+ def _posts_filter_category_id(posts, category_id, category_id_AND)
125
+ return posts unless category_id
126
+ category_ids = category_id.is_a?(Array) ? category_id : category_id.split(',')
127
+ posts = posts.where(lato_blog_categories: { id: category_ids })
128
+ # manage AND clause
129
+ if ['true', true].include?(category_id_AND)
130
+ ids = []
131
+ posts.pluck(:id).each do |id|
132
+ ids.push(id) if posts.where(id: id).length >= category_ids.length
133
+ end
134
+ posts = posts.where(id: ids)
135
+ end
136
+ # return posts
137
+ posts
138
+ end
139
+
140
+ def _posts_filter_search(posts, search)
141
+ return posts unless search
142
+ posts.where('lato_blog_posts.title like ?', "%#{params[:search]}%")
143
+ end
144
+
145
+ def _categories_filter_by_order(categories, order)
146
+ categories.order("title #{order}")
147
+ end
148
+
149
+ def _categories_filter_by_language(categories, language)
150
+ return categories unless language
151
+ categories.where(meta_language: language)
152
+ end
153
+
154
+ def _categories_filter_search(categories, search)
155
+ return categories unless search
156
+ categories.where('title like ?', "%#{search}%")
157
+ end
7
158
 
8
159
  end
9
160
 
@@ -1,3 +1,3 @@
1
1
  module LatoBlog
2
- VERSION = '2.1.1'
2
+ VERSION = '2.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lato_blog
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ideonetwork
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-26 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails