Permalink
Browse files

initial extraction

  • Loading branch information...
0 parents commit 5d7400f3745fb06820a66647178e088a3469688f @chrisrhoden committed May 23, 2010
18 Gemfile
@@ -0,0 +1,18 @@
+source :gemcutter
+
+# these gems are required.
+gem 'sinatra'
+gem 'activerecord', :require => 'active_record'
+gem 'haml'
+gem 'sqlite3'
+
+# You can add your own gems to this file
+# and then run bundle install to make sure they
+# are all there.
+
+group :test do
+ gem 'rspec', :require => 'spec/expectations'
+ gem 'capybara'
+ gem 'cucumber'
+ gem 'factory_girl'
+end
27 Rakefile
@@ -0,0 +1,27 @@
+namespace :db do
+ desc "Migrate the database"
+ task(:migrate => :environment) do
+ require 'logger'
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ ActiveRecord::Migration.verbose = true
+ ActiveRecord::Migrator.migrate("db/migrate")
+ end
+end
+
+desc 'Runs cucumber features for this project'
+task :cucumber do
+ exec "cucumber features"
+end
+
+desc 'Loads up this project\'s environment'
+task :environment do
+ require File.join(File.dirname(__FILE__), 'environment')
+end
+
+desc 'Runs irb in this project\'s context'
+task :irb do |t|
+ exec "irb -I lib -r '#{File.join(File.dirname(__FILE__), 'environment')}'"
+end
+
+desc 'Alias for irb'
+task :console => :irb
8 application.rb
@@ -0,0 +1,8 @@
+#!/usr/bin/env ruby
+require 'sinatra'
+load File.join(File.dirname(__FILE__), 'environment.rb')
+# Add your application code below this line.
+
+get '/' do
+ "<h1>Hello, world!</h1>"
+end
2 config.ru
@@ -0,0 +1,2 @@
+require File.join(File.dirname(__FILE__), 'application')
+run Sinatra::Application
1 db/.gitignore
@@ -0,0 +1 @@
+*.sqlite.db
0 db/migrate/.gitignore
No changes.
27 environment.rb
@@ -0,0 +1,27 @@
+require 'bundler'
+ENV['RACK_ENV'] ||= 'development'
+Bundler.require(:default, (ENV['RACK_ENV'].downcase.intern))
+$: << File.dirname(__FILE__)
+
+class Environment
+ def self.reload!
+ [:models, :lib].each do |dir|
+ Dir[File.join(File.dirname(__FILE__), dir.to_s, '*')].each do |file|
+ send(ENV['RACK_ENV'] == 'development' ? :load : :require, file) unless File.directory? file
+ end
+ end
+ end
+end
+
+Environment.reload!
+
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => "db/#{ENV['RACK_ENV'].downcase.underscore}.sqlite.db")
+
+
+configure :development do
+ before do
+ Environment.reload!
+ end
+end
+
+helpers TeamFun::Helpers
9 features/step_definitions/factory_steps.rb
@@ -0,0 +1,9 @@
+Given /^I have (\d+) (.+)?$/ do |number, klass|
+ number.to_i.times { Factory.create klass.underscore.singularize }
+end
+
+Given /^I have the following (.+):$/ do |klass, table|
+ table.hashes.each do |params|
+ Factory.create klass.underscore.singularize, params
+ end
+end
8 features/step_definitions/hello_world.feature
@@ -0,0 +1,8 @@
+Feature: Say Hello World!
+ In order to get a warm feeling
+ As a Sinatra and Cucumber User
+ I want to have a working installation
+
+ Scenario: Saying Hello!
+ When I go to "/"
+ Then I should see "Hello, world!" within "h1"
211 features/step_definitions/web_steps.rb
@@ -0,0 +1,211 @@
+require 'uri'
+require 'cgi'
+
+module WithinHelpers
+ def with_scope(locator)
+ locator ? within(locator) { yield } : yield
+ end
+end
+World(WithinHelpers)
+
+Given /^(?:|I )am at "(.+)"$/ do |page_name|
+ visit page_name
+end
+
+When /^(?:|I )go to "(.+)"$/ do |page_name|
+ visit page_name
+end
+
+When /^(?:|I )press "([^\"]*)"(?: within "([^\"]*)")?$/ do |button, selector|
+ with_scope(selector) do
+ click_button(button)
+ end
+end
+
+When /^(?:|I )follow "([^\"]*)"(?: within "([^\"]*)")?$/ do |link, selector|
+ with_scope(selector) do
+ click_link(link)
+ end
+end
+
+When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, value, selector|
+ with_scope(selector) do
+ fill_in(field, :with => value)
+ end
+end
+
+When /^(?:|I )fill in "([^\"]*)" for "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
+ with_scope(selector) do
+ fill_in(field, :with => value)
+ end
+end
+
+# Use this to fill in an entire form with data from a table. Example:
+#
+# When I fill in the following:
+# | Account Number | 5002 |
+# | Expiry date | 2009-11-01 |
+# | Note | Nice guy |
+# | Wants Email? | |
+#
+# TODO: Add support for checkbox, select og option
+# based on naming conventions.
+#
+When /^(?:|I )fill in the following(?: within "([^\"]*)")?:$/ do |selector, fields|
+ with_scope(selector) do
+ fields.rows_hash.each do |name, value|
+ When %{I fill in "#{name}" with "#{value}"}
+ end
+ end
+end
+
+When /^(?:|I )select "([^\"]*)" from "([^\"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
+ with_scope(selector) do
+ select(value, :from => field)
+ end
+end
+
+When /^(?:|I )check "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
+ with_scope(selector) do
+ check(field)
+ end
+end
+
+When /^(?:|I )uncheck "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
+ with_scope(selector) do
+ uncheck(field)
+ end
+end
+
+When /^(?:|I )choose "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, selector|
+ with_scope(selector) do
+ choose(field)
+ end
+end
+
+When /^(?:|I )attach the file "([^\"]*)" to "([^\"]*)"(?: within "([^\"]*)")?$/ do |path, field, selector|
+ with_scope(selector) do
+ attach_file(field, path)
+ end
+end
+
+Then /^(?:|I )should see JSON:$/ do |expected_json|
+ require 'json'
+ expected = JSON.pretty_generate(JSON.parse(expected_json))
+ actual = JSON.pretty_generate(JSON.parse(response.body))
+ expected.should == actual
+end
+
+Then /^(?:|I )should see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
+ with_scope(selector) do
+ if page.respond_to? :should
+ page.should have_content(text)
+ else
+ assert page.has_content?(text)
+ end
+ end
+end
+
+Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
+ regexp = Regexp.new(regexp)
+ with_scope(selector) do
+ if page.respond_to? :should
+ page.should have_xpath('//*', :text => regexp)
+ else
+ assert page.has_xpath?('//*', :text => regexp)
+ end
+ end
+end
+
+Then /^(?:|I )should not see "([^\"]*)"(?: within "([^\"]*)")?$/ do |text, selector|
+ with_scope(selector) do
+ if page.respond_to? :should
+ page.should have_no_content(text)
+ else
+ assert page.has_no_content?(text)
+ end
+ end
+end
+
+Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^\"]*)")?$/ do |regexp, selector|
+ regexp = Regexp.new(regexp)
+ with_scope(selector) do
+ if page.respond_to? :should
+ page.should have_no_xpath('//*', :text => regexp)
+ else
+ assert page.has_no_xpath?('//*', :text => regexp)
+ end
+ end
+end
+
+Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should contain "([^\"]*)"$/ do |field, selector, value|
+ with_scope(selector) do
+ field = find_field(field)
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
+ if field_value.respond_to? :should
+ field_value.should =~ /#{value}/
+ else
+ assert_match(/#{value}/, field_value)
+ end
+ end
+end
+
+Then /^the "([^\"]*)" field(?: within "([^\"]*)")? should not contain "([^\"]*)"$/ do |field, selector, value|
+ with_scope(selector) do
+ field = find_field(field)
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
+ if field_value.respond_to? :should_not
+ field_value.should_not =~ /#{value}/
+ else
+ assert_no_match(/#{value}/, field_value)
+ end
+ end
+end
+
+Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should be checked$/ do |label, selector|
+ with_scope(selector) do
+ field_checked = find_field(label)['checked']
+ if field_checked.respond_to? :should
+ field_checked.should == 'checked'
+ else
+ assert_equal 'checked', field_checked
+ end
+ end
+end
+
+Then /^the "([^\"]*)" checkbox(?: within "([^\"]*)")? should not be checked$/ do |label, selector|
+ with_scope(selector) do
+ field_checked = find_field(label)['checked']
+ if field_checked.respond_to? :should_not
+ field_checked.should_not == 'checked'
+ else
+ assert_not_equal 'checked', field_checked
+ end
+ end
+end
+
+Then /^(?:|I )should be on (.+)$/ do |page_name|
+ current_path = URI.parse(current_url).path
+ if current_path.respond_to? :should
+ current_path.should == path_to(page_name)
+ else
+ assert_equal path_to(page_name), current_path
+ end
+end
+
+Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
+ query = URI.parse(current_url).query
+ actual_params = query ? CGI.parse(query) : {}
+ expected_params = {}
+ expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
+
+ if actual_params.respond_to? :should
+ actual_params.should == expected_params
+ else
+ assert_equal expected_params, actual_params
+ end
+end
+
+Then /^show me the page$/ do
+ save_and_open_page
+end
25 features/support/env.rb
@@ -0,0 +1,25 @@
+ENV['RACK_ENV'] ||= 'test'
+app_file = File.join(File.dirname(__FILE__), *%w[.. .. application.rb])
+require app_file
+# Force the application name because polyglot breaks the auto-detection logic.
+Sinatra::Application.app_file = app_file
+require 'capybara/session'
+require 'capybara/cucumber'
+Capybara.app = Sinatra::Application
+Capybara.default_selector = :css
+
+Factory.definition_file_paths = [File.join(File.dirname(__FILE__), '..', 'factories')]
+Factory.find_definitions
+
+# Let's migrate our database automatically
+ActiveRecord::Base.logger = Logger.new(nil)
+ActiveRecord::Migration.verbose = true
+ActiveRecord::Migrator.migrate("db/migrate")
+
+Before do
+ tables = ActiveRecord::Base.connection.tables
+ tables.shift
+ tables.each do |table|
+ ActiveRecord::Base.connection.execute('DELETE FROM ' + table + ';')
+ end
+end
4 lib/team_fun.rb
@@ -0,0 +1,4 @@
+module TeamFun
+ require File.join( File.dirname(__FILE__), *%w{team_fun form_builder})
+ require File.join( File.dirname(__FILE__), *%w{team_fun helpers})
+end
57 lib/team_fun/form_builder.rb
@@ -0,0 +1,57 @@
+class ::TeamFun::FormBuilder
+
+ attr_reader :template, :options, :object
+
+ def initialize(options, object, template)
+ @options, @object, @template = options, object, template
+ end
+
+ [:text, :email, :password].each do |name|
+ define_method name.to_s + '_field' do |field|
+ input name, field
+ end
+ end
+
+ def textarea(field)
+ "<textarea id='#{obj_id}_#{field.to_s}'" +\
+ "#{class_with_errors(field)} name='#{input_name(field)}'>"+\
+ "#{object.send(field).to_s}</textarea>"
+ end
+
+ def input(type, field)
+ "<input id='#{obj_id}_#{field.to_s}'" +\
+ "#{class_with_errors(field)} name='#{input_name(field)}'"+\
+ " value='#{object.send(field).to_s}' type='#{type}' />"
+ end
+
+ def submit_button(text=nil)
+ "<button type='submit'>#{text||save_text}</button>"
+ end
+
+ def label(field, text=nil)
+ "<label for='#{obj_id}_#{field.to_s}'#{class_with_errors(field)}>#{text||field.to_s.humanize}</label>"
+ end
+
+ private
+
+ def class_with_errors(field)
+ " class='error'" if object.errors[field]
+ end
+
+ def obj_id
+ @obj_id ||= "#{object_name}#{'_'+object.id.to_s unless object.new_record?}"
+ end
+
+ def object_name
+ @object_name ||= object.class.name.underscore
+ end
+
+ def input_name(field)
+ object_name+"[#{field.to_s}]"
+ end
+
+ def save_text
+ object.new_record? ? 'Create' : 'Update'
+ end
+
+end
41 lib/team_fun/helpers.rb
@@ -0,0 +1,41 @@
+module ::TeamFun::Helpers
+ def form_for(object, options = {}, &block)
+ if options[:method] and options[:method].to_s != 'get'
+ method, options[:method] = options[:method], 'post'
+ end
+ if method.blank? or options[:action].blank?
+ unless object.new_record?
+ method ||= 'put'
+ options[:action] ||= uri_for(object)
+ else
+ options[:action] ||= "/#{object.class.name.pluralize.underscore}"
+ end
+ end
+
+ form = "<form method='#{options.delete(:method)||'post'}'#{options_to_attributes(options)}>\n"
+ form << haml_indent << " <input type='hidden' name='_method' value='#{method}' />\n" if method
+ block = capture_haml(FunFormBuilder.new(options, object, self), &block)
+ block.each_line do |line|
+ form << haml_indent + ' ' + line
+ end
+ form + "</form>"
+ end
+
+ def link_to(location, text = nil, options = {})
+ "<a href='#{uri_for(location)}'#{options_to_attributes(options)}>#{text||location.to_s}</a>"
+ end
+
+ def uri_for(object)
+ return object.to_uri if object.respond_to?(:to_uri)
+ return object.to_s if object.kind_of? String
+ "/#{object.class.name.pluralize.underscore}/#{object.id}"
+ end
+
+ def redirect_to(object)
+ redirect uri_for(object)
+ end
+
+ def options_to_attributes(options)
+ options.inject('') { |str, hash| str << " #{hash[0].to_s}='#{hash[1].to_s}'" }
+ end
+end
0 models/.gitignore
No changes.
0 views/.gitignore
No changes.

0 comments on commit 5d7400f

Please sign in to comment.