Testing Image Attachments/Uploads in Rails
Model
We need to use File.open(Rails.root.join("<image_path>"))
.
File.open(Rails.root.join("test/fixtures/files/sample.jpeg"))
inherit_from: ../.rubocop.yml | |
# Checks if uses of quotes match the configured preference. | |
Style/StringLiterals: | |
Enabled: false | |
We need to use File.open(Rails.root.join("<image_path>"))
.
File.open(Rails.root.join("test/fixtures/files/sample.jpeg"))
This is what we need to do:
export RAILS_ENV=production
rake db:reset
rake assets:precompile
RAILS_SERVE_STATIC_FILES=true
SECRET_KEY_BASE=production rails s
Add reference:
# add_reference :table, :column_name, foreign_key: boolean
add_reference :bookings, :flight, foreign_key: true
Add index:
def random_date_in_period(period)
rand(period).seconds.ago
end
random_date_in_period 1.year #=> Tue, 22 May 2018 04:55:54 UTC +00:00
random_date_in_period 1.year #=> Mon, 06 Nov 2017 19:50:07 UTC +00:00
# group.rb
# When we pass NULL to LIMIT, Postgres treats it as LIMIT ALL (no limit).
# https://www.postgresql.org/docs/current/static/sql-select.html#SQL-LIMIT
def topics_prioritized(normal_topics_limit: nil)
ids = topic_ids(normal_topics_limit)
def update_topics_comments_count
begin
ActiveRecord::Base.connection.execute <<~SQL
UPDATE topics
SET comments_count = (SELECT count(1)
FROM topic_comments
WHERE topic_comments.topic_id = topics.id
In Rails 5 we can set a default timestamp for a datetime column using a lambda with 'CURRENT_TIMESTAMP'
.
default: -> { 'CURRENT_TIMESTAMP' }
Creating a table:
# frozen_string_literal: true | |
class ObjectNameDecorator < SimpleDelegator | |
delegate :class, :is_a?, to: :__getobj__ | |
def self.collection(objects) | |
objects.map { |group| ObjectNameDecorator.new(object) } | |
end | |
private |