Skip to content

Instantly share code, notes, and snippets.

@rebelweb
rebelweb / application.html.erb
Created December 8, 2017 13:22
Webpacker / Bootstrap Config
<%# /app/views/layouts/application.html.erb %>
<!DOCTYPE html>
<html>
<head>
<title>RyancondronPhotography</title>
<%= csrf_meta_tags %>
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
@rebelweb
rebelweb / thing.rb
Last active May 1, 2018 18:20
raisl callback example
# frozen_String_literal: true
class Thing < ApplicationRecord
validates :name, presence: true
# can use before_create, before_save, before_update, after_save, after_create,
# after_update, before_validation, after_validation
# also accepts if/unless
before_save :set_bool #, if: proc { |thing| thing.name == 'TEST' }
@rebelweb
rebelweb / update_counter_cache_job.rb
Created October 17, 2019 11:42
Update Counter Cache Job
class UpdateCounterCacheJob < ApplicationJob attr_accessor :cache_columns
def perform(key = 'all')
self.cache_columns = counter_cache_hash
if key == 'all'
cache_columns.keys.each { |obj| update_cache_columns(obj) }
else
update_cache_columns(key)
end
end
{ "category": [ "article_categories" ] }
RSpec.describe UpdateCounterCachesJob, type: :job do
it 'updates all cache columns when they are out of sync' do sql = <<-SQL update categories SET articles_count = 5 SQL
category = create(:category)
ActiveRecord::Base.connection.execute(sql)
category.reload
expect(category.articles_count).to eq(5)
UpdateCounterCachesJob.perform_now
category.reload
expect(category.articles_count).to eq(0)
end
class Category < ApplicationRecord
has_many :article_categories, class_name: ArticleCategory, foreign_key: :category_id, dependent: :destroy
has_many :articles, through: :article_categories
end
class Article < ApplicationRecord
belongs_to :author, class_name: User, foreign_key: :author_id
has_many :article_categories, class_name: ArticleCategory, foreign_key: :article_id, dependent: :destroy
has_many :categories, through: :article_categories
has_many :images, class_name: ArticleImage, foreign_key: :article_id, dependent: :destroy
@rebelweb
rebelweb / FileShareClient.cs
Created November 17, 2019 03:21
SMB Library File Share Client using IDisposable
using System;
using System.Net;
using FileShare.Access.DTO;
using SMBLibrary;
using SMBLibrary.Client;
namespace FileShare.Access
{
public class FileShareClient : SMB2Client, IDisposable
{
@rebelweb
rebelweb / Service.cs
Created November 17, 2019 03:34
SMB File Share Access Service in C#
using System;
using System.Linq;
using System.Text;
using SMBLibrary;
namespace FileShare.Access
{
public interface IClientDTO
{
string IPAddress { get; set; }
@rebelweb
rebelweb / Program.cs
Created February 1, 2020 03:11
A simple C# Program to move data from one database to another
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace DataConverter
{
class Program
{
static string NpgsqlConnectionString => Environment.GetEnvironmentVariable("PSQL_CONN");
@rebelweb
rebelweb / DataContext.cs
Created February 1, 2020 03:15
Data Converter DbContext
using Microsoft.EntityFrameworkCore;
namespace DataConverter
{
public class DataConverterContext : DbContext
{
public DbSet<User> Users { get; set; }
public DataConverterContext(DbContextOptions opts) : base(opts) { }