Skip to content

Commit

Permalink
Install ActionText
Browse files Browse the repository at this point in the history
Add JS/CSS to the admin layout only.

To prevent deprecation warnings we set the following as this will be the
default in Rails 7.1:
  `config.active_storage.replace_on_assign_to_many = true`
  • Loading branch information
gbp committed Mar 21, 2024
1 parent e527ab0 commit 1fb78f2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
31 changes: 31 additions & 0 deletions app/assets/stylesheets/actiontext.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
* the trix-editor content (whether displayed or under editing). Feel free to incorporate this
* inclusion directly in any other asset bundle and remove this file.
*
*= require trix
*/

/*
* We need to override trix.css’s image gallery styles to accommodate the
* <action-text-attachment> element we wrap around attachments. Otherwise,
* images in galleries will be squished by the max-width: 33%; rule.
*/
.trix-content .attachment-gallery > action-text-attachment,
.trix-content .attachment-gallery > .attachment {
flex: 1 0 33%;
padding: 0 0.5em;
max-width: 33%;
}

.trix-content .attachment-gallery.attachment-gallery--2 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--2 > .attachment, .trix-content .attachment-gallery.attachment-gallery--4 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--4 > .attachment {
flex-basis: 50%;
max-width: 50%;
}

.trix-content action-text-attachment .attachment {
padding: 0 !important;
max-width: 100% !important;
}
2 changes: 2 additions & 0 deletions app/assets/stylesheets/admin.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//= require "actiontext"

/* As we're namespacing bootstrap to class admin, which is applied to the body
element in the admin interface (no id or class allowed on the HTML element
in HTML 4.01) and to the navbar also, so it can be styled with bootstrap
Expand Down
14 changes: 14 additions & 0 deletions app/views/active_storage/blobs/_blob.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>

<figcaption class="attachment__caption">
<% if caption = blob.try(:caption) %>
<%= caption %>
<% else %>
<span class="attachment__name"><%= blob.filename %></span>
<span class="attachment__size"><%= number_to_human_size blob.byte_size %></span>
<% end %>
</figcaption>
</figure>
3 changes: 3 additions & 0 deletions app/views/layouts/action_text/contents/_content.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="trix-content">
<%= yield -%>
</div>
3 changes: 3 additions & 0 deletions app/views/layouts/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<%= javascript_include_tag "admin" %>
<%= stylesheet_link_tag "admin", :title => "Main", :rel => "stylesheet" %>
<%= stylesheet_link_tag 'admin/print', :rel => "stylesheet", :media => "print" %>

<script type="text/javascript" src="https://unpkg.com/trix@2.0.8/dist/trix.umd.min.js"></script>

<%= render :partial => 'layouts/favicon' %>

</head>
Expand Down
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require "action_controller/railtie"
require "action_mailer/railtie"
# require "action_mailbox/engine"
# require "action_text/engine"
require "action_text/engine"
require "action_view/railtie"
# require "action_cable/engine"
require "sprockets/railtie"
Expand All @@ -34,6 +34,7 @@ class Application < Rails::Application
config.autoloader = :zeitwerk
config.active_record.legacy_connection_handling = false
config.active_support.use_rfc4122_namespaced_uuids = true
config.active_storage.replace_on_assign_to_many = true

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
Expand Down
29 changes: 29 additions & 0 deletions db/migrate/20240228122324_create_action_text_tables.action_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
def change
# Use Active Record's configured type for primary and foreign keys
primary_key_type, foreign_key_type = primary_and_foreign_key_types

create_table :action_text_rich_texts, id: primary_key_type do |t|
t.string :name, null: false
t.text :body, size: :long
t.references :record,
null: false, polymorphic: true, index: false, type: foreign_key_type

t.timestamps

t.index [:record_type, :record_id, :name],
name: "index_action_text_rich_texts_uniqueness", unique: true
end
end

private

def primary_and_foreign_key_types
config = Rails.configuration.generators
setting = config.options[config.orm][:primary_key_type]
primary_key_type = setting || :primary_key
foreign_key_type = setting || :bigint
[primary_key_type, foreign_key_type]
end
end

0 comments on commit 1fb78f2

Please sign in to comment.