site_blog 0.9.0 → 0.11.0

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
  SHA256:
3
- metadata.gz: 59fe18f766baca355592c03228df6214a9ca9370b46d8419c5819120abd487b9
4
- data.tar.gz: 3e06efe39ced409bdc71042e7a641a2b9d666b272c0e05045a287857dc845ab8
3
+ metadata.gz: 17887d197e4f068342e6b4b5488168131bf0322a10bd3ed5599db3c4133677e6
4
+ data.tar.gz: 6919603b9225bcdf09606af5faad5749f6864e83ce9996986e2cebd6b883ddf5
5
5
  SHA512:
6
- metadata.gz: 0bdeb913978e58662f36c7928a442a5ab24ef79fb28b262b3f04b0d363255fbc133383397f3efa1754055ee5c65430f56fffa2584a8f09d43903ddf1e704e15b
7
- data.tar.gz: 3a9de2774549820a8defc10c49ca65c338d97c6d1f95803bfbb9833559ca251a431fdf32f3d44d757f5b27d8d4b216cc3e32e1142181ee15b367c909a08eed25
6
+ metadata.gz: 6de7294cc0bc47e8d0b846abf65909a2d5d91f7f48c501d86ba9a691774e3370cbd2255b4538298dc2f5c57820dd738e795bfd437c8ee844943f2d05fc472ff1
7
+ data.tar.gz: 4ec3a1ef2ad9c2d8c5e951982e791eacd32580c5c27e3af300ad2abbdeb33f9ce6559019c8438712c19999d1cb9b09fe06ad8c9240fac53fa5f16919af88ded1
@@ -1,58 +1,31 @@
1
1
  module SiteBlog
2
2
  class ArticlesController < ApplicationController
3
- before_action :set_article, only: %i[ show update destroy ]
3
+ before_action :set_host, only: %i[ index show main ]
4
4
 
5
5
  # GET /articles
6
6
  def index
7
- render json: Article.filters(params)
7
+ @data = Article.filters(params)
8
+ render formats: :json
8
9
  end
9
10
 
11
+ # GET /articles/main
10
12
  def main
11
- render json: Article.main
13
+ @article = Article.main
14
+ render formats: :json
12
15
  end
13
16
 
14
- # GET /articles/1
17
+ # GET /articles/:slug
15
18
  def show
19
+ @article = Article.find_by(slug: params[:id])
16
20
  @article.increment!(:view_count)
17
- render json: @article
18
- end
19
-
20
- # POST /articles
21
- def create
22
- article = Article.new(article_params)
23
-
24
- if article.save
25
- render json: article, status: :created
26
- else
27
- render json: article.errors, status: :unprocessable_entity
28
- end
29
- end
30
-
31
- # PATCH/PUT /articles/1
32
- def update
33
- if @article.update(article_params)
34
- render json: @article
35
- else
36
- render json: @article.errors, status: :unprocessable_entity
37
- end
38
- end
39
-
40
- # DELETE /articles/1
41
- def destroy
42
- @article.destroy
43
- render json: { success: true }
21
+ render formats: :json
44
22
  end
45
23
 
46
24
  private
47
25
 
48
- # Use callbacks to share common setup or constraints between actions.
49
- def set_article
50
- @article = Article.find_by(params[:slug])
51
- end
26
+ def set_host
27
+ @host = !Rails.env.production? ? request.host : ''
28
+ end
52
29
 
53
- # Only allow a list of trusted parameters through.
54
- def article_params
55
- params.require(:article).permit(:title, :description, :image, :tag_id, :archived, :active)
56
- end
57
30
  end
58
31
  end
@@ -1,53 +1,16 @@
1
1
  module SiteBlog
2
2
  class TagsController < ApplicationController
3
- before_action :set_tag, only: %i[ show update destroy ]
4
3
 
5
4
  # GET /tags
6
5
  def index
7
- render json: Tag.all
6
+ render json: Tag.archived(false).as_json(except: [:archived])
8
7
  end
9
8
 
10
9
  # GET /tags/1
11
10
  def show
12
- render json: @tag
11
+ @tag = Tag.find(params[:id])
12
+ render json: @tag.as_json(except: [:archived])
13
13
  end
14
14
 
15
- # POST /tags
16
- def create
17
- @tag = Tag.new(tag_params)
18
-
19
- if @tag.save
20
- render json: @tag, status: :created
21
- else
22
- render json: @tag.errors, status: :unprocessable_entity
23
- end
24
- end
25
-
26
- # PATCH/PUT /tags/1
27
- def update
28
- if @tag.update(tag_params)
29
- render json: @tag
30
- else
31
- render json: @tag.errors, status: :unprocessable_entity
32
- end
33
- end
34
-
35
- # DELETE /tags/1
36
- def destroy
37
- @tag.destroy
38
- render json: { success: true }
39
- end
40
-
41
- private
42
-
43
- # Use callbacks to share common setup or constraints between actions.
44
- def set_tag
45
- @tag = Tag.find(params[:id])
46
- end
47
-
48
- # Only allow a list of trusted parameters through.
49
- def tag_params
50
- params.require(:tag).permit(:slug, :arichived)
51
- end
52
15
  end
53
16
  end
@@ -33,19 +33,19 @@ module SiteBlog
33
33
  data = archived(false)
34
34
  data = data.joins(:tags).where(tags: { id: params[:tag_id] }) if params[:tag_id].present?
35
35
  data = data.search(params[:query]) if params[:query].present?
36
- count = data.length
36
+ count = data.count
37
37
  data = data.order('created_at DESC')
38
- data = data.limit(params[:limit]) if params[:limit]
39
38
  data = data.page(params[:page]).per(20)
39
+ data = data.limit(params[:limit]) if params[:limit]
40
40
 
41
41
  {
42
42
  total_count: count,
43
- data: data
43
+ articles: data
44
44
  }
45
45
  }
46
46
 
47
47
  def self.main
48
- if self.archived(false).where(main: true).exist?
48
+ if self.archived(false).where(main: true).exists?
49
49
  res = self.archived(false).where(main: true).last
50
50
  else
51
51
  res = self.order('created_at DESC').first
@@ -7,6 +7,8 @@ module SiteBlog
7
7
 
8
8
  before_validation :slug_downcase
9
9
 
10
+ scope :archived, -> (param = true) { where(archived: param) }
11
+
10
12
  private
11
13
 
12
14
  def slug_downcase
@@ -0,0 +1,15 @@
1
+ json.total_count @data[:total_count]
2
+ json.data do
3
+ json.array! @data[:articles] do |article|
4
+ json.id article.id
5
+ json.title article.title
6
+ json.slug article.slug
7
+ json.html_description article.html_description
8
+ json.view_count article.view_count
9
+ json.main article.main
10
+ json.orig_image_url @host + article.image.url
11
+ json.thumb_image_url @host + article.image.thumb.url
12
+ json.(article, :created_at, :updated_at)
13
+ end
14
+ end
15
+
@@ -0,0 +1,16 @@
1
+ json.id @article.id
2
+ json.title @article.title
3
+ json.slug @article.slug
4
+ json.html_description @article.html_description
5
+ json.view_count @article.view_count
6
+ json.main @article.main
7
+ json.orig_image_url @host + @article.image.url
8
+ json.thumb_image_url @host + @article.image.thumb.url
9
+ json.tags do
10
+ json.array! @article.tags do |tag|
11
+ json.id tag.id
12
+ json.slug tag.slug
13
+ end
14
+ end
15
+ json.(@article, :created_at, :updated_at)
16
+
@@ -0,0 +1,16 @@
1
+ json.id @article.id
2
+ json.title @article.title
3
+ json.slug @article.slug
4
+ json.html_description @article.html_description
5
+ json.view_count @article.view_count
6
+ json.main @article.main
7
+ json.orig_image_url @host + @article.image.url
8
+ json.thumb_image_url @host + @article.image.thumb.url
9
+ json.tags do
10
+ json.array! @article.tags do |tag|
11
+ json.id tag.id
12
+ json.slug tag.slug
13
+ end
14
+ end
15
+ json.(@article, :created_at, :updated_at)
16
+
data/config/routes.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  SiteBlog::Engine.routes.draw do
2
2
  resources :tags
3
- resources :articles do
4
- get :main, on: :collection
5
- end
3
+ get 'articles/main' => 'articles#main'
4
+ resources :articles
6
5
  end
@@ -2,18 +2,18 @@ class AddArticleAndTag < ActiveRecord::Migration[7.0]
2
2
  def change
3
3
  create_table :site_blog_tags do |t|
4
4
  t.string :slug
5
- t.boolean :archived, null: false, default: false
5
+ t.boolean :archived, null: false, default: false, index: true
6
6
  t.timestamps
7
7
  end
8
8
 
9
9
  create_table :site_blog_articles do |t|
10
10
  t.string :title, null: false
11
- t.string :slug, null: false
11
+ t.string :slug, null: false, unique: true, index: true
12
12
  t.text :html_description, null: false
13
13
  t.text :description, null: false
14
14
  t.string :image, null: false
15
15
  t.integer :view_count, null: false, default: 0
16
- t.boolean :archived, null: false, default: false
16
+ t.boolean :archived, null: false, default: false, index: true
17
17
  t.boolean :active, null: false, default: true
18
18
  t.boolean :main, null: false, default: false
19
19
  t.string :locale, null: false
@@ -25,8 +25,5 @@ class AddArticleAndTag < ActiveRecord::Migration[7.0]
25
25
  t.belongs_to :tag, foreign_key: { to_table: :site_blog_tags }
26
26
  t.timestamps
27
27
  end
28
-
29
- add_index :site_blog_articles, :slug, unique: true
30
- add_index :site_blog_articles, [:slug, :archived]
31
28
  end
32
29
  end
@@ -1,3 +1,3 @@
1
1
  module SiteBlog
2
- VERSION = "0.9.0"
2
+ VERSION = "0.11.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_blog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eldar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-28 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jbuilder
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Description of AppComponent
98
112
  email:
99
113
  - eldarweb@mail.ru
@@ -118,6 +132,9 @@ files:
118
132
  - app/models/site_blog/tagging.rb
119
133
  - app/uploaders/site_blog/image_uploader.rb
120
134
  - app/views/layouts/blog/application.html.erb
135
+ - app/views/site_blog/articles/index.json.jbuilder
136
+ - app/views/site_blog/articles/main.json.jbuilder
137
+ - app/views/site_blog/articles/show.json.jbuilder
121
138
  - config/routes.rb
122
139
  - db/migrate/20230201140930_add_article_and_tag.rb
123
140
  - lib/site_blog.rb