APIdock - Notes posted to Ruby on Rails
270 FOLLOWERS
APIdock is a web app that provides a rich and usable interface for searching, perusing and improving the documentation of selected Ruby projects.
APIdock - Notes posted to Ruby on Rails
3y ago
Don’t let the depreciation warning scare you, Dirty lives under ActiveModel as of 3.0.0
apidock.com/rails/ActiveModel ..read more
APIdock - Notes posted to Ruby on Rails
3y ago
This private method returns the label used for the `f.submit` helper.
apidock.com/rails/v5.2.3/ActionView/Helpers/FormBuilder/submit
<%= form_for @post do |f| %>
<%= f.submit %>
<% end %>
In the example above, if @post is a new record, this method returns “Create Post” as submit button label; otherwise, it uses “Update Post”.
This method also checks for custom language using I18n under the helpers.submit key and using %{model} for translation interpolation:
en:
helpers:
submit:
create: "Create a %{model}"
update: "Confirm changes to %{model}"
It also s ..read more
APIdock - Notes posted to Ruby on Rails
3y ago
One of the handiest features of `f.submit` is the auto-generated label text.
Some styling frameworks don’t lend themselves to using `f.submit` without lots of tweaking.
If you’re not using `f.submit` but still want to access the label text, note that there is a private method:
f.send(:submit_default_value)
One example (in Haml, using MaterializeCss):
.row
.col.s6.center
%button{ type: 'submit', name: 'commit', data: { disable: { with: 'Submitting...' } }, class: 'waves-effect waves-light btn maroon' }
= f.send(:submit_default_value ..read more
APIdock - Notes posted to Ruby on Rails
3y ago
The statement about not loading a bunch of records is correct because pluck returns an Array of values, not a ActiveRecord::Relation or an Array of Records.
The exact wording though may be:
“…without retrieving from the database unused columns or loading a bunch of records just to grab the attributes you want ..read more
APIdock - Notes posted to Ruby on Rails
4y ago
@EdvardM added a note years ago saying this does not symbolize the keys of
deeply nested hashes, which may have been the case for whatever version of
ruby was available at the time, but in 4+, it definitely does work as
described:
hash = {"a" => :a, "b" => {z: [[{"c" => 3}, {"c" => 4}], []]}}
hash.deep_symbolize_keys
=> {:a=>:a, :b=>{:z=>[[{:c=>3}, {:c=>4 ..read more
APIdock - Notes posted to Ruby on Rails
5y ago
If you’re doing
expect_any_instance_of(ActiveRecord::Relation).to receive(:create_with)
and it does not work, try:
expect_any_instance_of(ActiveRecord::Associations::CollectionProxy).to receive(:create_with) { |proxy, attributes|
expect(proxy.klass).to eq(RecordClass)
expect(attributes[:....]).to eq(...)
double('find_or_create_by!' => Proc.new {})
}
or when testing “find_or_create_by”
expect_any_instance_of(ActiveRecord::Associations::CollectionProxy).to receive(:find_or_create_by) { |proxy, attributes|
expect(proxy.klass).to eq(RecordClass)
expect(attributes[:....]).to eq ..read more
APIdock - Notes posted to Ruby on Rails
5y ago
By default, to_sentence
combines elements inclusively by using ", and ".
If you want to be exclusive and combine the
elements using ", or ", you can either pass in lengthy
options to to_sentence or use this handy
extension I made to add
to_sentence_exclusive to the Array class.
class Array
# Adds a simple method that overloads `to_sentence` except it uses "or" instead of "and", which is the default.
# The reason for this is because `to_sentence` is extremely flexible but that results in a lot of code to get
# the simple result of using "or" instead of "and". This makes it simple ..read more