bongo 0.0.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
1
  module Bongo
2
- class ApplicationController < ActionController::Base
3
- protect_from_forgery with: :exception
2
+ class ApplicationController < ApplicationController
4
3
  end
5
4
  end
@@ -7,21 +7,32 @@ module Bongo
7
7
  before_action :set_article, only: [:show, :edit, :update, :destroy]
8
8
 
9
9
  def index
10
- @articles = Article.all.order(publish_at: :desc)
10
+ @articles = policy_scope(Article).published.order(publish_at: :desc)
11
+ respond_to do |format|
12
+ format.html
13
+ format.rss do
14
+ @articles = @articles.published.order(publish_at: :desc)
15
+ render layout: false
16
+ end
17
+ end
11
18
  end
12
19
 
13
20
  def show
21
+ authorize @article
14
22
  end
15
23
 
16
24
  def new
17
25
  @article = Article.new
26
+ authorize @article
18
27
  end
19
28
 
20
29
  def edit
30
+ authorize @article
21
31
  end
22
32
 
23
33
  def create
24
34
  @article = Article.new(article_params)
35
+ authorize @article
25
36
 
26
37
  if @article.save
27
38
  redirect_to @article, notice: 'Article was successfully created.'
@@ -31,6 +42,8 @@ module Bongo
31
42
  end
32
43
 
33
44
  def update
45
+ authorize @article
46
+
34
47
  if @article.update(article_params)
35
48
  redirect_to @article, notice: 'Article was successfully updated.'
36
49
  else
@@ -39,6 +52,7 @@ module Bongo
39
52
  end
40
53
 
41
54
  def destroy
55
+ authorize @article
42
56
  @article.destroy
43
57
  redirect_to articles_url, notice: 'Article was successfully destroyed.'
44
58
  end
@@ -0,0 +1,7 @@
1
+ module Bongo
2
+ class DraftsController < ArticlesController
3
+ def index
4
+ @drafts = policy_scope(Article).not.published.order(updated_at: :desc)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require_dependency "bongo/application_controller"
2
+
3
+ require "aws-sdk-s3"
4
+
5
+ module Bongo
6
+ class FilesController < ApplicationController
7
+ before_action :authenticate_user!
8
+
9
+ skip_before_action :verify_authenticity_token
10
+
11
+ def create
12
+ extension = File.extname(params[:file].original_filename)
13
+ s3 = Aws::S3::Resource.new
14
+ obj = s3.bucket(ENV["AWS_S3_BUCKET"]).object(SecureRandom.uuid + extension)
15
+ obj.upload_file(params[:file])
16
+
17
+ respond_to do |format|
18
+ format.json { render json: obj.public_url }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,9 +1,26 @@
1
1
  module Bongo
2
2
  class Article
3
3
  include Mongoid::Document
4
+ include Mongoid::Timestamps::Updated
5
+ include Mongoid::Slug
6
+
7
+ slug :title, history: true
4
8
 
5
9
  field :title, type: String
6
10
  field :text, type: String
7
11
  field :publish_at, type: Date
12
+
13
+ scope :published, -> { where(:publish_at.lte => Date.today) }
14
+
15
+ # Mongoid (v7.0.8) doesn't have a cache_version method on its document.
16
+ # This causes https://jira.mongodb.org/browse/MONGOID-4680 when trying to
17
+ # use Rails collection caching.
18
+ def cache_version
19
+ if respond_to?(:updated_at)
20
+ updated_at.utc.to_s(:usec)
21
+ else
22
+ nil
23
+ end
24
+ end
8
25
  end
9
26
  end
@@ -0,0 +1,29 @@
1
+ module Bongo
2
+ class ArticlePolicy < ApplicationPolicy
3
+ class Scope < Scope
4
+ def resolve
5
+ if user&.admin?
6
+ scope.all
7
+ else
8
+ scope.published
9
+ end
10
+ end
11
+ end
12
+
13
+ def show?
14
+ true
15
+ end
16
+
17
+ def create?
18
+ user&.admin?
19
+ end
20
+
21
+ def update?
22
+ user&.admin?
23
+ end
24
+
25
+ def destroy?
26
+ user&.admin?
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ <article>
2
+ <header>
3
+ <h2><%= link_to article.title, article %></h2>
4
+ <small><%= article.publish_at ? l(article.publish_at, format: :long) : "Unpublished draft"%></small>
5
+ </header>
6
+ <div class="trix-content">
7
+ <%= article.text.html_safe %>
8
+ </div>
9
+ </article>
@@ -15,7 +15,8 @@
15
15
  <%= form.text_field :title, required: true %>
16
16
 
17
17
  <%= form.label :text %>
18
- <%= form.text_area :text, required: true %>
18
+ <%= form.hidden_field :text %>
19
+ <trix-editor input="article_text" class="trix-content" required></trix-editor>
19
20
 
20
21
  <%= form.label :publish_at %>
21
22
  <%= form.date_field :publish_at %>
@@ -23,6 +24,6 @@
23
24
  <button type="submit">Save</button>
24
25
 
25
26
  <%= link_to articles_path do %>
26
- <em>Back</em>
27
+ <em>Cancel</em>
27
28
  <% end %>
28
29
  <% end %>
@@ -0,0 +1 @@
1
+ <hr>
@@ -1,14 +1,9 @@
1
- <% @articles.each do |article| %>
2
- <article>
3
- <h2><%= link_to article.title, article %></h2>
4
- <small><%= article.publish_at ? l(article.publish_at, format: :long) : "Unpublished draft"%></small>
5
- <p><%= simple_format truncate(article.text, length: 500) %></p>
6
- </article>
7
- <br>
8
- <% end %>
1
+ <header>
2
+ <% if policy(Bongo::Article).create? %>
3
+ <%= link_to drafts_url do %>
4
+ <i>Drafts</i>
5
+ <% end %>
6
+ <% end %>
7
+ </header>
9
8
 
10
- <br>
11
-
12
- <%= link_to new_article_path do %>
13
- <b>New Article</b>
14
- <% end %>
9
+ <%= render(collection: @articles, partial: 'article', spacer_template: "hr", cached: true) || "There are no articles yet." %>
@@ -0,0 +1,18 @@
1
+ xml.instruct! :xml, version: "1.0"
2
+ xml.rss version: "2.0" do
3
+ xml.channel do
4
+ xml.title (Front.config.name rescue "My Blog")
5
+ xml.description (Front.config.description rescue "A bongo blog")
6
+ xml.link root_url
7
+
8
+ @articles.each do |article|
9
+ xml.item do
10
+ xml.title article.title
11
+ xml.description ActionView::Base.full_sanitizer.sanitize(article.text).truncate(180)
12
+ xml.pubDate article.publish_at.to_s(:rfc822)
13
+ xml.link article_url(article)
14
+ xml.guid article_url(article)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,21 +1,19 @@
1
- <article>
2
- <header>
3
- <h2><%= @article.title %></h2>
4
- <small><%= @article.publish_at ? l(@article.publish_at, format: :long) : "Unpublished draft"%></small>
5
- </header>
6
- <main><%= simple_format @article.text %></main>
7
- </article>
1
+ <%= render @article %>
8
2
 
9
- <br>
3
+ <footer>
4
+ <% if policy(@article).update? %>
5
+ <%= link_to edit_article_path(@article) do %>
6
+ <b>Edit</b>
7
+ <% end %>
8
+ <% end %>
10
9
 
11
- <%= link_to edit_article_path(@article) do %>
12
- <b>Edit</b>
13
- <% end %>
10
+ <% if policy(@article).destroy? %>
11
+ <%= link_to @article, method: :delete, data: {confirm: "Are you sure?"} do %>
12
+ <em>Delete</em>
13
+ <% end %>
14
+ <% end %>
14
15
 
15
- <%= link_to @article, method: :delete, data: {confirm: "Are you sure?"} do %>
16
- <em>Delete</em>
17
- <% end %>
18
-
19
- <%= link_to articles_path do %>
20
- <em>Back</em>
21
- <% end %>
16
+ <%= link_to articles_path do %>
17
+ <i>Back</i>
18
+ <% end %>
19
+ </footer>
@@ -0,0 +1,16 @@
1
+ <header>
2
+ <% if policy(Bongo::Article).create? %>
3
+ <%= link_to new_article_path do %>
4
+ <b>New Article</b>
5
+ <% end %>
6
+ <% end %>
7
+ </header>
8
+
9
+ <ul>
10
+ <% @drafts.each do |draft| %>
11
+ <li>
12
+ <%= link_to draft.title, draft %><br>
13
+ <small>last modified <%= distance_of_time_in_words_to_now(draft.updated_at) %> ago</small>
14
+ </li>
15
+ <% end %>
16
+ </ul>
@@ -2,4 +2,8 @@
2
2
  Blog
3
3
  <% end %>
4
4
 
5
+ <% content_for :head do %>
6
+ <%= auto_discovery_link_tag :rss, articles_url(format: :rss) %>
7
+ <% end %>
8
+
5
9
  <%= render template: "layouts/application" %>
@@ -1,4 +1,6 @@
1
1
  Bongo::Engine.routes.draw do
2
2
  root to: "articles#index"
3
3
  resources :articles
4
+ resources :drafts
5
+ resources :files, only: :create
4
6
  end
@@ -1,3 +1,4 @@
1
+ require "mongoid_slug"
1
2
  require "bongo/engine"
2
3
 
3
4
  module Bongo
@@ -1,3 +1,3 @@
1
1
  module Bongo
2
- VERSION = '0.0.2'
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Hutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-17 00:00:00.000000000 Z
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,49 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 7.0.5
41
- description: Bongo is a mountable Rails engine for blogging using MongoDB.
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid-slug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '6'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk-s3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pundit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ description: Rails engine for blogging using MongoDB.
42
84
  email:
43
85
  - tohu@tuta.io
44
86
  executables: []
@@ -49,27 +91,38 @@ files:
49
91
  - README.md
50
92
  - Rakefile
51
93
  - app/assets/config/bongo_manifest.js
94
+ - app/assets/javascripts/bongo/application.js
95
+ - app/assets/javascripts/bongo/attachments.js.erb
96
+ - app/assets/javascripts/bongo/trix.js
52
97
  - app/assets/stylesheets/bongo/application.css
98
+ - app/assets/stylesheets/bongo/trix.css
53
99
  - app/controllers/bongo/application_controller.rb
54
100
  - app/controllers/bongo/articles_controller.rb
101
+ - app/controllers/bongo/drafts_controller.rb
102
+ - app/controllers/bongo/files_controller.rb
55
103
  - app/helpers/bongo/application_helper.rb
56
104
  - app/helpers/bongo/articles_helper.rb
57
105
  - app/jobs/bongo/application_job.rb
58
106
  - app/mailers/bongo/application_mailer.rb
59
107
  - app/models/bongo/article.rb
108
+ - app/policies/bongo/article_policy.rb
109
+ - app/views/bongo/articles/_article.html.erb
60
110
  - app/views/bongo/articles/_form.html.erb
111
+ - app/views/bongo/articles/_hr.html
61
112
  - app/views/bongo/articles/_notice.html.erb
62
113
  - app/views/bongo/articles/edit.html.erb
63
114
  - app/views/bongo/articles/index.html.erb
115
+ - app/views/bongo/articles/index.rss.builder
64
116
  - app/views/bongo/articles/new.html.erb
65
117
  - app/views/bongo/articles/show.html.erb
118
+ - app/views/bongo/drafts/index.html.erb
66
119
  - app/views/layouts/bongo/application.html.erb
67
120
  - config/routes.rb
68
121
  - lib/bongo.rb
69
122
  - lib/bongo/engine.rb
70
123
  - lib/bongo/version.rb
71
124
  - lib/tasks/bongo_tasks.rake
72
- homepage: https://rubygems.org/gems/bongo
125
+ homepage: https://github.com/thutterer/bongo
73
126
  licenses:
74
127
  - MIT
75
128
  metadata: {}
@@ -88,8 +141,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
141
  - !ruby/object:Gem::Version
89
142
  version: '0'
90
143
  requirements: []
91
- rubygems_version: 3.1.2
144
+ rubygems_version: 3.0.3
92
145
  signing_key:
93
146
  specification_version: 4
94
- summary: A blogging engine
147
+ summary: A blogging engine for Rails
95
148
  test_files: []