richer_text 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e3e144daed92cf04465df7c11e55a30bec134bd6abd083b2ce76b078eb579323
4
+ data.tar.gz: 348c16817e8d8faad41627f985bb6d4dbf7392cd67d04b9faed369cd2ade73a5
5
+ SHA512:
6
+ metadata.gz: 4e73645fe681761bc2941cc3814f4ecc2e84c6ad55060e4a62e84351393a6e6fe089a511c4e1a92072c52c6bc9a971d754706df22b57604789d3efc0dd44359e
7
+ data.tar.gz: 78bfe77159991d5449f5bfb57dd9727bc5aafa87d648f06913089e791ca69165eab938f8095c82f408a78727c1620f1011fff99803db8dcb7ff53ca22c97f8ee
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 Andrea Fomera
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RicherText
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "richer_text"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install richer_text
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/richer_text .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module RicherText
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RicherText
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RicherText
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RicherText
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module RicherText
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ module RicherText
2
+ class RichText < ApplicationRecord
3
+ belongs_to :record, polymorphic: true
4
+
5
+ serialize :body, RicherText::Content
6
+
7
+ delegate :to_s, :nil?, to: :body
8
+
9
+ has_many_attached :images
10
+
11
+ before_save :update_images
12
+
13
+ def to_html
14
+ body&.to_html
15
+ end
16
+
17
+ private
18
+
19
+ def update_images
20
+ self.images = body.image_blobs if body.present?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Richer text</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "richer_text/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RicherText::Engine.routes.draw do
2
+ end
@@ -0,0 +1,13 @@
1
+ class CreateRicherTextRichTexts < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :richer_text_rich_texts do |t|
4
+ t.string :name, null: false
5
+ t.text :body, size: :long
6
+ t.references :record, null: false, polymorphic: true
7
+
8
+ t.timestamps
9
+
10
+ t.index [:record_type, :record_id, :name], name: "index_richer_texts_rich_texts_uniqueness", unique: true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ module RicherText
2
+ module Attribute
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def has_richer_text(name)
7
+ class_eval <<-CODE, __FILE__, __LINE__ + 1
8
+ def #{name}
9
+ richer_text_#{name} || build_richer_text_#{name}
10
+ end
11
+
12
+ def #{name}?
13
+ richer_text_#{name}.present?
14
+ end
15
+
16
+ def #{name}=(body)
17
+ self.#{name}.body = body
18
+ end
19
+ CODE
20
+
21
+ has_one :"richer_text_#{name}", -> { where(name: name) },
22
+ class_name: "RicherText::RichText", as: :record, inverse_of: :record, autosave: true, dependent: :destroy
23
+
24
+ scope :"with_richer_text_#{name}", -> { includes("richer_text_#{name}") }
25
+ end
26
+
27
+ # Eager load all dependent RichText models in bulk.
28
+ def with_all_richer_text
29
+ eager_load(richer_text_association_names)
30
+ end
31
+
32
+ def richer_text_association_names
33
+ reflect_on_all_associations(:has_one).collect(&:name).select { |n| n.start_with?("richer_text_") }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ module RicherText
2
+ class Content
3
+ include Serialization
4
+
5
+ delegate :blank?, :empty?, :html_safe, :present?, to: :to_html
6
+
7
+ def initialize(body)
8
+ @body = body
9
+
10
+ @fragment = RicherText::Fragment.parse(body)
11
+ end
12
+
13
+ def inspect
14
+ "#<#{self.class.name} #{to_html.truncate(25).inspect}>"
15
+ end
16
+
17
+ def to_s
18
+ to_html
19
+ end
20
+
21
+ def to_html
22
+ body
23
+ end
24
+
25
+ def images
26
+ @images ||= fragment.find_all("img").map do |img|
27
+ img.attributes["src"].value
28
+ end.uniq
29
+ end
30
+
31
+ def image_blobs
32
+ image_blob_ids.map { |id| ActiveStorage::Blob.find_signed(id) }
33
+ end
34
+
35
+ def image_blob_ids
36
+ images.map do |image|
37
+ # Ensure old images from ActionText which used the proxy url are supported
38
+ image = image.gsub("/proxy/", "/redirect/")
39
+ # Fetch the signed_blob_id from the image src
40
+ image.split("/rails/active_storage/blobs/redirect/").last.split("/").first
41
+ end
42
+ end
43
+
44
+ attr_reader :body, :fragment
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ require "rails"
2
+ require "nokogiri"
3
+
4
+ module RicherText
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace RicherText
7
+
8
+ initializer "richer_text.attribute" do
9
+ ActiveSupport.on_load(:active_record) do
10
+ include RicherText::Attribute
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module RicherText
2
+ class Fragment
3
+ class << self
4
+ def parse(html)
5
+ new(Nokogiri::HTML.fragment(html))
6
+ end
7
+ end
8
+
9
+ attr_reader :source
10
+
11
+ def initialize(source)
12
+ @source = source
13
+ end
14
+
15
+ def find_all(selector)
16
+ source.css(selector)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module RicherText
2
+ module Serialization
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def load(content)
7
+ new(content) if content
8
+ end
9
+
10
+ def dump(content)
11
+ case content
12
+ when nil
13
+ nil
14
+ when self
15
+ content.to_html
16
+ when RicherText::RichText
17
+ content.body.to_html
18
+ else
19
+ new(content).to_html
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module RicherText
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "active_support"
2
+ require "active_support/rails"
3
+
4
+ require "richer_text/version"
5
+ require "richer_text/engine"
6
+
7
+ module RicherText
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Attribute
11
+ autoload :Content
12
+ autoload :Fragment
13
+ autoload :Serialization
14
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :richer_text do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: richer_text
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrea Fomera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ description:
28
+ email:
29
+ - afomera@hey.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/richer_text_manifest.js
38
+ - app/assets/stylesheets/richer_text/application.css
39
+ - app/controllers/richer_text/application_controller.rb
40
+ - app/helpers/richer_text/application_helper.rb
41
+ - app/jobs/richer_text/application_job.rb
42
+ - app/mailers/richer_text/application_mailer.rb
43
+ - app/models/richer_text/application_record.rb
44
+ - app/models/richer_text/rich_text.rb
45
+ - app/views/layouts/richer_text/application.html.erb
46
+ - config/routes.rb
47
+ - db/migrate/20230107020316_create_richer_text_rich_texts.rb
48
+ - lib/richer_text.rb
49
+ - lib/richer_text/attribute.rb
50
+ - lib/richer_text/content.rb
51
+ - lib/richer_text/engine.rb
52
+ - lib/richer_text/fragment.rb
53
+ - lib/richer_text/serialization.rb
54
+ - lib/richer_text/version.rb
55
+ - lib/tasks/richer_text_tasks.rake
56
+ homepage: https://github.com/afomera/richer_text
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/afomera/richer_text
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.4.10
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: A richer text editor for Rails
80
+ test_files: []