Skip to content

Instantly share code, notes, and snippets.

stephan.com stephancom

Block or report user

Report or block stephancom

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@stephancom
stephancom / migrate_fields_to_action_text.rb
Created Apr 27, 2019
migrate to actiontext (untested)
View migrate_fields_to_action_text.rb
class MigrateFieldsToActionText < ActiveRecord::Migration[5.2]
class ActionTextRichText < ActiveRecord::Base
belongs_to :record, polymorphic: true
end
TARGETS = {
ModelName => [:field],
OtherModelName => %i[field1 field2 field3]
}.freeze
def up
@stephancom
stephancom / en.yml
Created Apr 20, 2019
not rewritable validator
View en.yml
en:
errors:
messages:
changed: "can't be changed"
@stephancom
stephancom / copy_protect.coffee
Created Apr 20, 2019
block copy/paste etc on web page
View copy_protect.coffee
# __ __ __ __ __ __ ___ ___ __ ___ __
# / ` / \ |__) \ / |__) |__) / \ | |__ / ` | | / \ |\ |
# \__, \__/ | | | | \ \__/ | |___ \__, | | \__/ | \|
#
# https://stackoverflow.com/a/32804545/444955
$ ->
$.fn.blockCopy = (options) ->
settings = $.extend({ blockPasteClass: null }, options)
style_appender = (rule) ->
@stephancom
stephancom / write_once_field.rb
Created Apr 19, 2019
"write once" fields in rails. Can be set on create or ONCE on update, cannot be changed in mass assignment
View write_once_field.rb
validate :forbid_changing_some_field, on: :update
def forbid_changing_some_field
return unless some_field_changed?
return if some_field_was.nil?
self.some_field = some_field_was
errors.add(:some_field, 'can not be changed!')
end
View block_queue.rb
class BlockQueue
def initialize(name)
@name = name
@blocks = []
end
def run_later(&block)
Progress.log "queueing job for #{@name}"
@blocks << block
end
@stephancom
stephancom / populate.rb
Created Mar 22, 2019
"populate" file header
View populate.rb
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-. #
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / #
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' #
# .-------. ,-----. .-------. ___ _ .---. ____ ,---------. .-''-. #
# \ _(`)_ \ .' .-, '. \ _(`)_ \.' | | | | ,_| .' __ `.\ \ .'_ _ \ #
# | (_ o._)| / ,-.| \ _ \ | (_ o._)|| .' | |,-./ ) / ' \ \`--. ,---'/ ( ` ) ' #
# | (_,_) /; \ '_ / | :| (_,_) /.' '_ | |\ '_ '`) |___| / | | \ . (_ o _) | #
# | '-.-' | _`,/ \ _/ || '-.-' ' ( \.-.| > (_) ) _.-` | :_ _: | (_,_)___| #
# | | : ( '\_/ \ ;| | ' (`. _` /|( . .-' .' _ | (_I_) ' \ .---. #
# | | \ `"/ \ ) / | | | (_ (_) _) `-'`-'|___ | _( )_ | (_(=)_) \ `-' / #
@stephancom
stephancom / thank_you.txt
Created Mar 11, 2019
ascii thank you suitable for facebook response even on mobile
View thank_you.txt
!!!
┏┓┏┓╋╋╋╋╋╋┏┓
┃┗┫┗┳━┓┏━┳┫┣┓
┃┏┫┃┃╋┗┫┃┃┃━┫
┗━┻┻┻━━┻┻━┻┻┛
╋╋╋╋╋╋┏┳┓
┏┳┳━┳┳┫┃┃
┃┃┃╋┃┃┃┃┃
┣┓┣━┻━╋╋┫
┗━┛╋╋╋┗┻┛
@stephancom
stephancom / destructive.rake
Last active May 7, 2019
guard for destructive rake tasks
View destructive.rake
# _ _ _ _
# __| | ___ ___| |_ _ __ _ _ ___| |_(_)_ _____
# / _` |/ _ \/ __| __| '__| | | |/ __| __| \ \ / / _ \
# | (_| | __/\__ \ |_| | | |_| | (__| |_| |\ V / __/
# \__,_|\___||___/\__|_| \__,_|\___|\__|_| \_/ \___|
# handy guard for destructive rake tasks
# just include this before any troublesome task
# typical usage:
# task :riskytask, [:destructive, :environment] do
@stephancom
stephancom / stdio.rb
Created Feb 11, 2019
Handy stdio helpers for Rspec - suppress output and fake input
View stdio.rb
# _ _ _ _ _
# ___| |_ __| (_) ___ | |__ ___| |_ __ ___ _ __ ___
# / __| __/ _` | |/ _ \ | '_ \ / _ \ | '_ \ / _ \ '__/ __|
# \__ \ || (_| | | (_) | | | | | __/ | |_) | __/ | \__ \
# |___/\__\__,_|_|\___/ |_| |_|\___|_| .__/ \___|_| |___/
# |_| by stephan.com
# especially handy for testing interactive rake tasks
# best in an rspec around block, and can even be chained eg:
#
View table_helpers.rb
module TableHelpers
module ArrayMethods
def find_row(expected_row)
find_index do |row|
expected_row.all? do |expected_column|
first_column = row.find_index { |column|
content = normalize_content(column.content)
expected_content = normalize_content(expected_column)
matching_parts = expected_content.split(/\s*\*\s*/, -1).collect { |part| Regexp.escape(part) }
matching_expression = /\A#{matching_parts.join(".*")}\z/
You can’t perform that action at this time.