Skip to content

Instantly share code, notes, and snippets.

View workmad3's full-sized avatar

David Workman workmad3

View GitHub Profile
class InlineFormsController < ApplicationController
def self.cancan_enabled?
return @cancan_enabled if instance_variables.include?(:@cancan_enabled)
::Ability
@cancan_enabled = true
rescue NameError
@cancan_enabled = false
end
def cancan_enabled?
ree-1.8.7-2011.03 :005 > Card.new
=> #<Card _id: 4ec4f9ff31ef62a8ee000003>
ree-1.8.7-2011.03 :001 > u = User.new
=> #<User _id: 4ec4f9ea31ef62a8ee000001, email: nil>
ree-1.8.7-2011.03 :003 > u.cards
NoMethodError: undefined method `cards' for #<User:0x12d3170d0>
headlines = ["one","two","three","flee"]
#enumerables have a method #to_phrase.rhyme_key which gives a key based on pronunciation.
So headlines.map(&:to_phrase.rhyme_key) => 1,2,3,3 (for example)
#Now I want to filter out all the strings that don't have rhyme_key counterparts,
essentially leaving ["three","flee"] in the convoluted example above. I tried the code below,
but that just stores the rhyme_key values in the headlines array which is useless to me because
the original string literals get replaced with the values. How do I check against the values via
the methods without replacing the original strings?
begin
ddowns = $browser.find_elements(:css, ".n-dropdown.sizer")
loading = ddowns.any?{|ddown|ddown.include? 'Loading'})
end while loading
weekdays = %w(Sun Mon Tue Wed Thu Fri Sat)
capture do
content_tag :ul do
weekdays.each do |day|
concat do
content_tag(:li, day)
end
end
end
@workmad3
workmad3 / gist:3195011
Created July 28, 2012 22:22 — forked from brian-mann/gist:3194911
Dynamically call model methods to chain includes
## Customer.rb
def self.references
includes(:account)
end
def self.forms
includes(:forms)
end
class OtoplastikCustomer < Customer
def otoplastik?
true
end
def prices(article)
price_base = OtoplastikArticlePrice.where(otoplastik_article_id: article.id)
[id, price_group, price_list].map{|i| price_base.where(mechant_code: i)}.find(&:exists?)
end
end
# spec/factories/users.rb
FactoryGirl.define do
sequence(:email) { |n| "user#{n}@puppa.pup" }
sequence(:partitaiva) { 11.times.map { (0..9).to_a.sample }.join }
factory :user do
email
password 'secret'
password_confirmation 'secret'
phone '0999711333'
@workmad3
workmad3 / replace.rb
Created December 4, 2012 17:04
Replace " with '
def replacer(filename, find = ?", replace = ?')
File.open(filename, "r+") do |file|
file.each do |line|
file.seek(-line.bytes.to_a.size, IO::SEEK_CUR)
file.write line.gsub(find, replace)
end
end
end
respond_to do |format|
format.html
format.xls do
render :xls => "some_filename", :template => "reports/#{params[:id]}.xls.writeexcel"
end
end