Skip to content

Instantly share code, notes, and snippets.

View glowacki-dev's full-sized avatar
🐧
🐦

Maciej Głowacki glowacki-dev

🐧
🐦
View GitHub Profile
@glowacki-dev
glowacki-dev / offsetting.rb
Created October 10, 2017 07:24
Using predefined id when creating models
offset = User.maximum(:id)
...
uid = comment.user_id + offset
Comment.new(commentable: postable,
text: comment.text,
user_id: uid).save(validate: false)
@glowacki-dev
glowacki-dev / import.rake
Last active October 10, 2017 07:26
Basic import rake task
task :import do
OtherUser.find_each do |other_user|
user = User.create!(email: other_user.email,
name: other_user.name)
other_user.image_posts.each do |other|
image = Image.create!(file_path: other.file_path,
file_size: other.file_size,
height: other.height,
width: other.width)
Post.create!(postable: image,
@glowacki-dev
glowacki-dev / other_user.rb
Created October 10, 2017 07:21
Creating a model with a connection to another database
class OtherUser < ApplicationRecord
establish_connection :competitor
self.table_name = :users
has_many :video_posts, class_name: 'OtherVideoPost', foreign_key: 'user_id'
has_many :image_posts, class_name: 'OtherImagePost', foreign_key: 'user_id'
has_many :text_posts, class_name: 'OtherTextPost', foreign_key: 'user_id'
has_many :profile_comments, class_name: 'OtherProfileComment', foreign_key: 'user_id'
end
@glowacki-dev
glowacki-dev / database.yml
Created October 10, 2017 07:20
Configuring foreign database
competitor:
adapter: mysql2
database: competitor_prod
username: secret_user
password: secret_password
@glowacki-dev
glowacki-dev / posts_spec.rb
Last active August 10, 2017 14:03
Final short form of posts test
require 'acceptance_helper'
resource 'Posts' do
explanation 'Posts are entities holding some text information.
They can be created and seen by anyone'
post '/posts' do
with_options scope: :post do
parameter :title, 'Title of a post. Can be empty'
parameter :body, 'Main text of a post. Must be longer than 10 letters', required: true
@glowacki-dev
glowacki-dev / posts_spec.rb
Last active August 10, 2017 14:03
Final long form of posts test
require 'acceptance_helper'
resource 'Posts' do
explanation 'Posts are entities holding some text information.
They can be created and seen by anyone'
post '/posts' do
with_options scope: :post do
parameter :title, 'Title of a post. Can be empty'
parameter :body, 'Main text of a post. Must be longer than 10 letters', required: true
@glowacki-dev
glowacki-dev / posts_spec.rb
Last active September 3, 2020 07:19
Example of doing multiple requests in a single test example based on previous version (https://gist.github.com/glowacki-dev/1e07df3cc75d73e6855b39316172d7ad)
require 'acceptance_helper'
resource 'Posts' do
explanation 'Posts are entities holding some text information.
They can be created and seen by anyone'
post '/posts' do
# ... same as before ...
let(:title) { 'Foo' }
@glowacki-dev
glowacki-dev / shared_examples_for_api_requests.rb
Last active September 3, 2020 07:19
Overriding parameters when making requests in previous api request shared example (https://gist.github.com/glowacki-dev/af3947a0cfa22fbb544981d68ab7aefd)
shared_examples 'api_requests' do |name, explanation|
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
example name do
explanation explanation
Array.wrap(subject).each do |request|
# ... Other stuff happening here ...
@glowacki-dev
glowacki-dev / api_request.rb
Last active September 3, 2020 07:19
Allowing overriding params in previous ApiRequest implementation (https://gist.github.com/glowacki-dev/9e0278ff0b826ca1198fce14293443a9)
class ApiRequest
attr_reader :status,
:response_keys,
:response_spec,
:specs,
:messages,
:stubs,
:params
def initialize
shared_examples 'api_requests' do |name, explanation|
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
example name do
explanation explanation
Array.wrap(subject).each do |request|
ActionMailer::Base.deliveries = []