Skip to content

Instantly share code, notes, and snippets.

View kermit-klein's full-sized avatar
🏠
Working from home

Ali Erbay kermit-klein

🏠
Working from home
View GitHub Profile
@kermit-klein
kermit-klein / oop.js
Created October 5, 2020 04:34
OOPvsFunc
class Validator {
static REQUIRED = "REQUIRED";
static MIN_LENGTH = "MIN_LENGTH";
static validate(value, flag, validatorValue) {
if (flag === this.REQUIRED) {
return value.trim().length > 0;
}
if (flag === this.MIN_LENGTH) {
return value.trim().length > validatorValue;
}
@kermit-klein
kermit-klein / func.js
Last active October 5, 2020 01:21
OPPvsFunc
const REQUIRED = "REQUIRED";
const MIN_LENGTH = "MIN_LENGTH";
const validate = (value, flag, validatorValue) => {
if (flag === REQUIRED) {
return value.trim().length > 0;
}
if (flag === MIN_LENGTH) {
return value.trim().length > validatorValue;
}
@kermit-klein
kermit-klein / manifest.rb
Created September 22, 2020 11:46
ActiveRecord2
class Manifest < ActiveRecord::Base
validates :name, presence: true
validates :description, presence: true
def outdate
self.status = :outdated
end
def active?
self.status != :outdated
@kermit-klein
kermit-klein / manifest.rb
Created September 22, 2020 11:23
ActiveRecord2
class Manifest < ActiveRecord::Base
validates :name, presence: true
validates :description, presence: true
end
@kermit-klein
kermit-klein / has_many_through.rb
Created September 22, 2020 11:14
ActiveRecord2
class Assembly < ApplicationRecord
has_many :manifests
has_many :parts, through: :manifests
end
class Manifest < ApplicationRecord
belongs_to :assembly
belongs_to :part
end
class Assembly < ApplicationRecord
has_and_belongs_to_many :parts
end
class Part < ApplicationRecord
has_and_belongs_to_many :assemblies
end
@kermit-klein
kermit-klein / migration.rb
Created September 22, 2020 06:41
ActiveRecord2
class CreateReviews < ActiveRecord::Migration[6.0]
def change
create_table :reviews do |t|
t.references :reviewable, polymorphic: true, index: true
t.string :body
t.timestamps
end
end
end
@kermit-klein
kermit-klein / polymorphic.rb
Last active September 22, 2020 06:27
ActiveRecord2
class Review < ApplicationRecord
# name the interface to the Review model reviewable
# establish that it is polymorphic
belongs_to :reviewable, polymorphic: true
end
class Restaurant < ApplicationRecord
# establish a relationship to the Review model
# via the reviewable interface
has_many :reviews, as: :reviewable
@kermit-klein
kermit-klein / has_and_belongs_to_many.rb
Last active September 14, 2020 09:49
ActiveRecord
class User
has_and_belongs_to_many :groups
end
class Group
has_and_belongs_to_many :users
end
@kermit-klein
kermit-klein / has_one_through.rb
Last active September 14, 2020 08:53
ActiveRecord
class User
has_one :profile
has_one :avatar, through: :profile
end
class Profile
belongs_to :user
has_one :avatar
end