Skip to content

Instantly share code, notes, and snippets.

class Product < ActiveRecord::Base
has_many :orders
validates :name, presence: true
validates :stock_level, presence: true
validates :min_stock_level, presence: true
before_validation :strip_name
before_save :notify_if_low_on_stock
after_save :notify_product_created
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id if current_user
if @comment.save
StatusWorker.perform_aync(@comment.id)
NotifyChannelsWorker.perform_async(@comment.id)
AuthorMailer.notify_mail(@post.author.id).deliver_later
@lucasmedeirosleite
lucasmedeirosleite / service.rb
Created October 30, 2018 14:38
Service module
module Service
extend ActiveSupport::Concern
included do
def self.call(*args)
new.call(*args)
end
def call
raise '#call method not implemented'
class Array
def sequential_search(element)
each do |e|
return e if e == element
end
nil
end
def binary_search(element)
@lucasmedeirosleite
lucasmedeirosleite / flattener.rb
Last active December 16, 2019 21:56
Flatten implementation in ruby without using it own method.
require 'rspec'
class Flattener
def initialize(array)
raise 'An Array must be passed' unless array.kind_of? Array
@array = array
end
def flatten
do_flatten(@array)