This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# get file name arg from command line | |
File.open(ARGV[0], 'r') do | f | | |
# read the first line containing the integer | |
(1..f.readline.strip.to_i).each do | i | | |
# div by 3? | |
if ( i % 3 ) == 0 then | |
# and by 5? | |
if ( i % 5 ) == 0 then |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
DIRECTORY_QUERY_URL_FOR = "https://web.middlebury.edu/database/directory/?cn=" | |
"A".upto("Z") do |letter| | |
url = "#{DIRECTORY_QUERY_URL_FOR}#{letter}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WidgetsController < ApplicationController | |
def destroy | |
@widget = Widget.find(params[:id]) | |
respond_to do |format| | |
if @widget.delete | |
format.html { flash[:notice] = "Item Removed"; redirect_to widgets_path } | |
# change format.json to format.js and it works properly | |
format.js { render :status => :ok } | |
else | |
format.html { flash[:alert] = "Something went terribly wrong"; redirect_to widgets_path } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
url: this.href, | |
type: 'POST', | |
dataType: 'script', | |
data: {_method: 'delete'}, | |
beforeSend: function() { console.log("in before send ...") }, | |
complete: function() { console.log("in complete ...") } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given that I have a model Item that inherits from ActiveRecord::Base and it has an attribute #locked_at, | |
# I want to be able to select T/F from a select in UI to set #locked_at's timestamp | |
# === VIEW === | |
# app/views/items/edit.html.haml, using formtastic | |
# Attempt #1: | |
# I could set locked_at => Time.now by setting it in the select's collection, | |
# but revisiting this form will not show the properly selected option, unless you do :selected => Time.now, | |
# which seems kludgy. Worse, if unlocked (locked_at => nil), this select option will not be |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Givens: Rails 3.0.7, Ruby 1.9.2, with RSpec2 (2.6.4) and FactoryGirl (1.3.3) | |
# original test ... | |
require 'spec_helper' | |
describe Item do | |
context 'a trivial example' do | |
before do | |
@item = Factory.create(:item) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# autoload concerns | |
module YourApp | |
class Application < Rails::Application | |
config.autoload_paths += %W( | |
#{config.root}/app/controllers/concerns | |
#{config.root}/app/models/concerns | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
# Class that takes a set of records from a Rails app and returns CSV'd goodness | |
# Record's must implement class method #csv_exportable_headers and instance method #csv_exportable_attributes | |
# Note: This probably should be moved to a module where the headers / attributes are defined | |
class Exporter | |
module CSV | |
def self.export(records) | |
# TODO: dropout if records is blank | |
klass = records.model_name.constantize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Client required that *all* methods for controllers are covered and | |
# do not allow guest access. | |
# | |
# To remove code duplication, add controller macro, update test suite. | |
# Macro defaults to :html formatted requests and handles member vs collection (assumes :id) | |
# Note that we can specify methods, formats, params, etc. along the lines of: | |
# it_should_block_access_to :index, :format => :json | |
# it_should_block_access_to :something, :method => :post | |
# it_should_block_access_to :index, :foo => :bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Client required that *all* routes for controllers are covered and | |
# do not allow guest access. | |
# | |
# There are several other controllers that, for guest test coverage, | |
# are identical to the Items Controller spec below | |
# Note: there are several non-CRUD methods; destroy makes a record | |
# inactive; there is no record removal revealed via the API (client's request) | |
# spec/controllers/items_controller_spec.rb | |
require 'spec_helper' |
OlderNewer