Skip to content

Instantly share code, notes, and snippets.

View BenMorganIO's full-sized avatar
😀
Why is this here?

Ben A. Morgan BenMorganIO

😀
Why is this here?
View GitHub Profile
@BenMorganIO
BenMorganIO / product_varitans_by_option_values.rb
Created September 15, 2014 17:36
Create a hash for the variants of a product based on the options value
Spree::Product.class_eval do
def variants_hash
product_variants = self.variants.to_a
color_hash = {}
option_value_variants = -> (color_id) do
sizes_hash = {}
option_values_for('Size').map do |option_value|
size_variants = option_value.variants.to_a.keep_if do |variant|
@BenMorganIO
BenMorganIO / product_variants.rb
Created September 19, 2014 22:57
Creating a factorial hash of variants of a product to JS in Spree
def product_variants
products = Spree::Taxon.find_by(name: 'Donations').products
product_hash = {}
variants = -> (product) do
variant_hash = {}
option_values = -> (option_type) do
option_value_hash = {}
@BenMorganIO
BenMorganIO / application_controller.rb
Created September 27, 2014 07:14
Dart2JS Rails Caching Issue
# For some reason you have to delete the cache beforehand
# when you use the dart2js gem with Rails.
# Just add this before_action in your controller and you're
# good
class ApplicationController < ActionController::Base
before_action :remove_old_dart_files
protect_from_forgery with: :exception
@BenMorganIO
BenMorganIO / cryptic_ruby.rb
Created December 4, 2014 19:19
cryptic ruby
def order_params
params[:order] &&= params[:order].permit(*permitted_order_attributes) or {}
end
@BenMorganIO
BenMorganIO / lib_tasks_karma.rb
Created January 12, 2015 02:41
Karma with Rails
namespace :karma do
task start: :environment do
with_tmp_config :start
end
task run: :environment do
with_tmp_config :start, "--single-run"
end
private
@BenMorganIO
BenMorganIO / application_service.rb
Last active August 29, 2015 14:13
ApplicationService for controllers
module ApplicationService
extend ActiveSupport::Concern
included do
attr_accessor :request
def self.call(*args)
# The last caller should be a controller and by using
# the binding_of_caller gem, we can grab that instance.
ctrl = binding.of_caller(1)
@BenMorganIO
BenMorganIO / orders_controller_decorator.rb
Last active August 29, 2015 14:13
orders create for non users
Spree::Api::OrdersController.class_eval do
# Remove the default authorize, we could move this functionality into
# cancan later though...
# authorize! :create, Order
def create
order_user = if @current_user_roles.include?('admin') && order_params[:user_id]
Spree.user_class.find(order_params[:user_id])
else
current_api_user
end
@BenMorganIO
BenMorganIO / test
Created January 24, 2015 00:01
Test script
#!/usr/bin/env ruby
class Test
RUBOCOP = "rubocop"
COFFEELINT = "coffeelint ."
RSPEC = "spring rspec spec"
KARMA = "spring rake karma:run"
BRAKEMAN = "brakeman -A --summary -q"
def initialize
nickel-demo $ cargo run --verbose
Fresh pkg-config v0.1.6
Compiling rust-mustache v0.3.0 (https://github.com/nickel-org/rust-mustache.git#fd5d0e1c)
Running `rustc /Users/benmorgan/.cargo/git/checkouts/rust-mustache-b850d4795ca6321d/master/src/lib.rs --crate-name mustache --crate-type lib -g -C metadata=48f7150e9c575dbb -C extra-filename=-48f7150e9c575dbb --out-dir /Users/benmorgan/Sites/sandbox/rust/nickel-demo/target/deps --emit=dep-info,link -L dependency=/Users/benmorgan/Sites/sandbox/rust/nickel-demo/target/deps -L dependency=/Users/benmorgan/Sites/sandbox/rust/nickel-demo/target/deps -Awarnings`
Fresh nickel_macros v0.0.1 (https://github.com/nickel-org/nickel.rs.git#4e7b5dda)
Fresh rustc-serialize v0.2.9
Fresh phantom v0.0.3
Fresh unsafe-any v0.2.1
Fresh matches v0.1.2
Fresh libc v0.1.0
@BenMorganIO
BenMorganIO / checkout_service.rb
Last active August 29, 2015 14:15
Services for Spree
module Spree
class CheckoutService
include ApplicationService
attr_reader :order, :order_params
def call(order, order_params)
@order, @order_params = order, order_params
order.email ||= order.user.email