Skip to content

Instantly share code, notes, and snippets.

View sergii's full-sized avatar

Serhii Ponomarov sergii

View GitHub Profile
# app/filters/base_filter.rb
class BaseFilter
include ActiveModel::Model
include ActiveModel::Attributes
def initialize(session)
@_session = session
super(@_session.fetch(:filters, {})[filter_resource_class])
end
@sergii
sergii / 01-board.rb
Created May 6, 2022 09:50 — forked from julianrubisch/01-board.rb
StimulusReflex Patterns - 1 Forms
# app/models/board.rb
class Board < ApplicationRecord
has_many :embeds
end
@sergii
sergii / membership_product.rb
Created May 6, 2022 09:49 — forked from seanharmer/membership_product.rb
Example usage of new delegated_type facility
class MembershipProduct < ApplicationRecord
include Sellable
# Other stuff...
end
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["source"];
copy() {
const range = document.createRange();
window.getSelection().removeAllRanges();
@sergii
sergii / git-commit-template.md
Created January 14, 2022 16:14 — forked from lisawolderiksen/git-commit-template.md
Use a Git commit message template to write better commit messages

Using Git Commit Message Templates to Write Better Commit Messages

The always enthusiastic and knowledgeable mr. @jasaltvik shared with our team an article on writing (good) Git commit messages: How to Write a Git Commit Message. This excellent article explains why good Git commit messages are important, and explains what constitutes a good commit message. I wholeheartedly agree with what @cbeams writes in his article. (Have you read it yet? If not, go read it now. I'll wait.) It's sensible stuff. So I decided to start following the

@sergii
sergii / description.markdown
Created December 21, 2021 16:56 — forked from runemadsen/description.markdown
Reverse polymorphic associations in Rails

Polymorphic Associations reversed

It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media

This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.

@sergii
sergii / html_safe.md
Created November 16, 2021 12:23 — forked from joekur/html_safe.md
Proper Use of `html_safe`

Proper use of html_safe

Let's look at an innocuous piece of ruby. Consider some view code showing a user's name and phone number:

"#{first_name} #{last_name} #{phone}"

Great - this is very succinct, readable, and can easily be extracted to a method in a

@sergii
sergii / friendly_urls.markdown
Created August 20, 2021 08:52 — forked from jcasimir/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@sergii
sergii / alias_matchers.md
Created August 18, 2021 15:59 — forked from JunichiIto/alias_matchers.md
List of alias matchers in RSpec 3

This list is based on aliases_spec.rb.

You can see also Module: RSpec::Matchers API.

matcher aliased to description
a_truthy_value be_truthy a truthy value
a_falsey_value be_falsey a falsey value
be_falsy be_falsey be falsy
a_falsy_value be_falsey a falsy value
@sergii
sergii / 1. nginx.conf
Created August 13, 2021 11:37 — forked from mezbahalam/1. nginx.conf
ruby on rails multiple custom domains over Https using Openresty and lua (https://www.babar.im/blog/sysadmin/serve-your-saas-customer-domains-over-https-for-free.html)
## /etc/openresty/nginx.conf
user www-data;
events {
worker_connections 1024;
}
http {
lua_shared_dict auto_ssl 1m;