-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrails_7.txt
219 lines (145 loc) · 8.06 KB
/
rails_7.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Rails 7 Whats New:
======================================================================================================
https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a-vision
Asstes
--------
- Using importmap instead of webpack (webpack instroduced in rails 6, before it was Sprockets)
- Rails 7 replaces Webpacker with importmapped Hotwire as default JavaScript setup
Fortunately, the web now is making it possible to reduce some of the tension surrounding Javascript development in Rails.
- The first major shift is major browser support for ES6. This completes removes the need for transpiling.
- The second crucial change is HTTP2. With HTTP2, there is no longer a need to pay a large penalty for sending many small files instead of one big file. A single connection can multiplex all the responses needed. Further reducing the need for one large Javascript bundle file.
- Finally, we have the era of import maps, which allow the use of logical references for modules in ES6. Previously, every Javascript file after compilation would look something like “main-a6d26cef87d241eba5fa.js”, basically the filename with a digest at the end. This means that every time any small change occurred in that file, it needs to be compiled all over again and create a new digest so that the browser recognizes the update. The answer to this problem is import-maps.
* https://github.com/wicg/import-maps
New options:
------------
rails new produces a skeleton configured with importmap-rails, turbo-rails, and stimulus-rails. No Webpack, no Yarn, no package.json, no node. All the integration setup is done through adding the gems, then running importmap:install, turbo:install, stimulus:install tasks.
rails new —skip-hotwire a skeleton-like above but with just importmap-rails configured.
rails new —skip-javascript no JavaScript setup of any kind is configured.
rails new —webpack is essentially what we had on Rails 6.2, with the exception that the default Turbolinks configuration is replaced with Hotwire (both Turbo and Stimulus). This of course includes setting up package.json, Yarn, etc.
rails new —webpack —skip-hotwire is the same as above, minus the default configuration of Hotwire.
Others:
-------
- Encryption at Database Layer
- Asynchronous Querying
- Zeitwerk Mode for Rails 7
- Retry Jobs Unlimited Times
- Named Variants
You can now name variants on ActiveStorage instead of specifying size on every access.
- Hash to HTML Attributes
<input <%= tag.attributes(type: :text, aria: { label: "Search" }) %>> => <input type="text" aria-label="Search">
- Assert a Single Record with sole
When querying records, you can now call sole or find_sole_by (instead of first or find_by) when you want to assert that the query should only match a single record.
Product.where(["price = %?", price]).sole
# => ActiveRecord::RecordNotFound (if no Product with given price)
# => #<Product ...> (if one Product with given price)
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
user.api_keys.find_sole_by(key: key)
# as above
- Check Presence/Absence of an Association
# Before:
account.users.joins(:contact).where.not(contact_id: nil)
# After:
account.users.where.associated(:contact)
- Stream Generated Files from Controller Actions
send_stream(filename: "subscribers.csv") do |stream|
stream.write "email_address,updated_at\n"
@subscribers.find_each do |subscriber|
stream.write "#{subscriber.email_address},#{subscriber.updated_at}\n"
end
end
- Named Variants (ActiveStorage)
# You can now name variants on ActiveStorage instead of specifying size on every access.
class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :thumb, resize: "100x100"
end
end
#Call avatar.variant(:thumb) to get a thumb variant of an avatar:
<%= image_tag user.avatar.variant(:thumb) %>
Extra
====================================================================================================================================
ActionText
------------
> bin/rails action_text:install
> bundle
> rails db:migrate
// application.js
import "trix"
import "@rails/actiontext"
bin/rails generate model Message content:rich_text
# app/models/message.rb
class Message < ApplicationRecord
has_rich_text :content
end
<%# app/views/messages/_form.html.erb %>
<%= form_with model: message do |form| %>
<div class="field">
<%= form.label :content %>
<%= form.rich_text_area :content %>
</div>
<% end %>
- And finally, display the sanitized rich text on a page:
<%= @message.content %>
-To accept the rich text content, all you have to do is permit the referenced attribute:
class MessagesController < ApplicationController
def create
message = Message.create! params.require(:message).permit(:title, :content)
redirect_to message
end
end
- By default, the Action Text editor and content are styled by the Trix defaults.
- If you want to change these defaults, remove the // require "actiontext.scss" line from your application.scss to omit the contents of that file.
- By default, Action Text will render rich text content into an element that declares the .trix-content class:
<%# app/views/layouts/action_text/contents/_content.html.erb %>
<div class="trix-content">
<%= yield %>
</div>
- Rendering attachments
In addition to attachments uploaded through Active Storage, Action Text can embed anything that can be resolved by a Signed GlobalID.
# https://guides.rubyonrails.org/action_text_overview.html
Security
-------------------------------------------------------------------------------------------
- As announced in Rails 5.2.0 release candidate, Rails 5.2 will go out with a brand new credentials API that will eventually replace the current config/secrets.yml and config/secrets.yml.enc.
- Two files
config/credentials.yml.enc # store all your private credentials, only decryoted via master.key
config/master.key # RAILS_MASTER_KEY, this file won;t be onl github, but will be generated via
> EDITOR=vim rails credentials:edit --environment production
> RAILS_ENV=production EDITOR=vim rails credentials:edit
# both files will be generated if they are not exist
- So, after running
aws:
access_key_id: 123
secret_access_key: 345
secret_key_base: 2fdea1259c6660852864f9726616df64c8cd
# Then, you should be able to access the configuration programmatically like this:
Rails.application.credentials.config # {:aws=>{:access_key_id=>"123", :secret_access_key=>"345"} }}
Rails.application.credentials.aws[:access_key_id]
Rails.application.credentials.aws[:access_key_id] # => "123"
Rails.application.credentials.aws[:secret_access_key] # => "345"
Rails.application.credentials.secret_key_base # => "2fdea...
# you can also
development:
aws:
access_key_id: 123
secret_access_key: 345
secret_key_base: 2fdea1259c6660852864f9726616df64c8cd
production:
aws:
access_key_id: 321
secret_access_key: 543
secret_key_base: hu23ih41iu23h4123u4h23iu4h2323j412i3
# so per enviroment
Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]
- Deploying master key
- When you move your code to a server, you need to make sure that your config/credentials.yml.enc file can be decrypted.
- That means that somehow you’ll need to provide Rails with your master key, given that it is not checked into version control. aha !!
- There are two ways of doing that:
- Option 1: Place the config/master.key file in the server.
You’ll normally want to symlink this file to a shared folder in the server filesystem.
Again, do not version your config/master.key file.
- Option 2: create a RAILS_MASTER_KEY ENV variable.
Rails will detect it and use it as your master key, e.g. in heroku: heroku config:set RAILS_MASTER_KEY=<your-master-key-here>.
# Setting master key on Heroku
heroku config:set RAILS_MASTER_KEY=`cat config/credentials/production.key`
- in production.rb
config.require_master_key = true