Skip to content

Instantly share code, notes, and snippets.

# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
scope :published, -> { where(published: true) }
end
@deepakmahakale
deepakmahakale / active_admin.scss
Created February 17, 2019 17:32
Active admin numeric range / number range filter
form.filter_form .filter_form_field.filter_numeric_range input[type='text'] {
background-position: 100% 3px;
background-repeat: no-repeat;
padding-right: 25px;
width: calc(50% - 3px);
}
form.filter_form .filter_form_field.filter_numeric_range .separator, form.filter_form .filter_form_field.filter_numeric_range input[type='text'] {
display: inline-block;
float: none;
@deepakmahakale
deepakmahakale / commands.md
Last active November 13, 2018 15:37
StatsD + influxDB + telegraf

Install StatsD

Make sure nodejs is installed

git clone https://github.com/etsy/statsd.git
cd statsd
cp exampleConfig.js config.js
subl config.js
node stats.js config.js
@deepakmahakale
deepakmahakale / _order_quantity.html.erb
Last active August 30, 2018 04:49
Spree promotions order quantity rule
<%#- app/views/spree/admin/promotions/rules/_order_quantity.html.erb %>
<div class='panel-body'>
<div class='row no-marginb'>
<div class='form-group col-xs-12 col-md-6 no-marginb'>
<%= select_tag "#{param_prefix}[preferred_operator_min]", options_for_select(Spree::Promotion::Rules::OrderQuantity::OPERATORS_MIN.map{|o| [Spree.t("order_quantity_rule.operators.#{o}"),o]}, promotion_rule.preferred_operator_min), { class: 'select2 select_item_total marginb' } %>
<%= select_tag "#{param_prefix}[preferred_operator_max]", options_for_select(Spree::Promotion::Rules::OrderQuantity::OPERATORS_MAX.map{|o| [Spree.t("order_quantity_rule.operators.#{o}"),o]}, promotion_rule.preferred_operator_max), { class: 'select2 select_item_total' } %>
</div>
<div class='form-group col-xs-12 col-md-6 no-marginb'>
<%= text_field_tag "#{param_prefix}[preferred_quantity_min]", promotion_rule.preferred_quantity_min, class: 'form-control marginb' %>
<%= text_field_tag "#{param_prefix}[preferred_quantity_

Change root password:

sudo passwd

Change user password:

passwd
require 'csv'
input_file = "#{Rails.root}/High School Database CSV.csv"
file_1 = CSV.open("#{Rails.root}/High_School_Database_#{1}.csv", 'w')
file_2 = CSV.open("#{Rails.root}/High_School_Database_#{2}.csv", 'w')
file_3 = CSV.open("#{Rails.root}/High_School_Database_#{3}.csv", 'w')
count = 0
CSV.foreach(input_file) do |row|
if count < 400000
file_1 << row
elsif count < 800000
@deepakmahakale
deepakmahakale / new css and js
Created July 5, 2018 05:17
find new css and js in two branches
git diff --color --name-status master..develop | grep -E '^A.*.js|^A.*.css' # js & css
git diff --color --name-status master..develop | grep -E '^A.*db/migrate/.*.rb' # migrations
@deepakmahakale
deepakmahakale / reversible_migration.md
Created July 5, 2018 05:16
Reversible migration for dropping a table
class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users do |t|
      t.string :email, null: false
      t.timestamps null: false
    end
  end
end
@deepakmahakale
deepakmahakale / rspec_model_testing_template.rb
Created July 5, 2018 05:15 — forked from SabretWoW/rspec_model_testing_template.rb
Rails Rspec model testing skeleton & cheat sheet using rspec-rails, shoulda-matchers, shoulda-callbacks, and factory_girl_rails. Pretty much a brain dump of examples of what you can (should?) test in a model. Pick & choose what you like, and please let me know if there are any errors or new/changed features out there. Reddit comment thread: http…
# This is a skeleton for testing models including examples of validations, callbacks,
# scopes, instance & class methods, associations, and more.
# Pick and choose what you want, as all models don't NEED to be tested at this depth.
#
# I'm always eager to hear new tips & suggestions as I'm still new to testing,
# so if you have any, please share!
#
# @kyletcarlson
#
# This skeleton also assumes you're using the following gems:
@deepakmahakale
deepakmahakale / scoped_association.md
Created July 5, 2018 05:15
Avoid N + 1 queries for association

Preload scoped associations

Rails provide different ways to load associations from database to avoid N + 1 query being fired.

I have two models User and Post with following associations:

# app/models/user.rb
class User < ActiveRecord::Base
  has_many :posts
end