Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit 1943e5e3e7fc8d136eec435dd3d495eb0c4d9521 @jim jim committed Mar 23, 2012
@@ -0,0 +1,4 @@
+.bundle/
+log/*.log
+pkg/
+.DS_Store
@@ -0,0 +1,9 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in carmen-rails.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gem 'carmen', path: '/Users/jimb/code/carmen'
+gem 'ruby-debug19'
+
+gemspec
@@ -0,0 +1,115 @@
+PATH
+ remote: .
+ specs:
+ carmen-rails (0)
+ carmen
+ rails
+
+PATH
+ remote: /Users/jimb/code/carmen
+ specs:
+ carmen (1.0.0.pre)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.2)
+ actionpack (= 3.2.2)
+ mail (~> 2.4.0)
+ actionpack (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ activerecord (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ activesupport (3.2.2)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ archive-tar-minitar (0.5.2)
+ arel (3.0.2)
+ builder (3.0.0)
+ columnize (0.3.4)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.3)
+ json (1.6.5)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.18)
+ minitest (2.11.2)
+ multi_json (1.1.0)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.2)
+ actionmailer (= 3.2.2)
+ actionpack (= 3.2.2)
+ activerecord (= 3.2.2)
+ activeresource (= 3.2.2)
+ activesupport (= 3.2.2)
+ bundler (~> 1.0)
+ railties (= 3.2.2)
+ railties (3.2.2)
+ actionpack (= 3.2.2)
+ activesupport (= 3.2.2)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.32)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ carmen!
+ carmen-rails!
+ minitest
+ ruby-debug19
@@ -0,0 +1,20 @@
+Copyright 2012 YOURNAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+= CarmenRails
+
+This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,19 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:spec) do |t|
+ t.libs << 'lib'
+ t.libs << 'spec'
+ t.pattern = 'spec/**/*_spec.rb'
+ t.verbose = false
+end
+
+task :default => :spec
@@ -0,0 +1,23 @@
+$:.push File.expand_path("../lib", __FILE__)
+
+# Maintain your gem's version:
+#require "carmen-rails/version"
+
+# Describe your gem and declare its dependencies:
+Gem::Specification.new do |s|
+ s.name = "carmen-rails"
+ s.version = '0' #CarmenRails::VERSION
+ s.authors = ["TODO: Your name"]
+ s.email = ["TODO: Your email"]
+ s.homepage = "TODO"
+ s.summary = "TODO: Summary of CarmenRails."
+ s.description = "TODO: Description of CarmenRails."
+
+ s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
+ s.test_files = Dir["test/**/*"]
+
+ s.add_dependency "rails"
+ s.add_dependency "carmen" #version
+
+ s.add_development_dependency "minitest"
+end
@@ -0,0 +1,9 @@
+require 'carmen/action_view/form_helper'
+
+module Carmen
+ module Rails
+ #class Railtie < ::Rails::Railtie
+ # perform rails setup
+ #end
+ end
+end
@@ -0,0 +1,74 @@
+module ActionView
+ module Helpers
+ module FormOptionsHelper
+
+ # Return select and option tags for the given object and method.
+ #
+ # Uses region_options_or_select to generate the list of option tags.
+ def subregion_select(object, method, parent_region_or_code, options={}, html_options={})
+ parent_region = case parent_region_or_code
+ when String
+ Carmen::Country.coded(parent_region_or_code)
+ when Array
+ parent_region_or_code.inject(Carmen::World.instance) { |parent, next_code|
+ parent.subregions.coded(next_code)
+ }
+ else
+ parent_region_or_code
+ end
+ tag = InstanceTag.new(object, method, self, options.delete(:object))
+ tag.to_region_select_tag(parent_region, options, html_options)
+ end
+
+ # Return select and option tags for the given object and method.
+ #
+ # Uses region_options_or_select to generate the list of option tags.
+ def country_select(object, method, options={}, html_options = {})
+ InstanceTag.new(object, method, self, options.delete(:object)).to_region_select_tag(Carmen::World.instance, options, html_options)
+ end
+
+ def region_options_for_select(parent_region, selected = nil, priority_region_codes)
+ region_options = ""
+
+ unless priority_region_codes.empty?
+ priority_regions = priority_region_codes.map do |code|
+ region = parent_region.subregions.coded(code)
+ [region.name, region.code] if region
+ end.compact
+ unless priority_regions.empty?
+ region_options += options_for_select(priority_regions, selected)
+ region_options += "<option disabled>-------------</option>"
+ end
+ end
+
+ main_options = parent_region.subregions.map { |r| [r.name, r.code] }
+ region_options += options_for_select(main_options, selected)
+ region_options.html_safe
+ end
+ end
+
+ class InstanceTag
+ def to_region_select_tag(parent_region, options, html_options)
+ html_options = html_options.stringify_keys
+ add_default_name_and_id(html_options)
+ priority_regions = options[:priority] || []
+ value = value(object)
+ opts = add_options(region_options_for_select(parent_region, value, priority_regions), options, value)
+ content_tag("select", opts, html_options)
+ end
+ end
+
+ class FormBuilder
+ def country_select(method, options = {}, html_options = {})
+ @template.country_select(@object_name, method,
+ options.merge(:object => @object), html_options)
+ end
+
+ def subregion_select(method, parent_region_or_code, options={}, html_options={})
+ @template.subregion_select(@object_name, method, parent_region_or_code,
+ options.merge(:object => @object), html_options)
+ end
+ end
+
+ end
+end
@@ -0,0 +1,3 @@
+module CarmenRails
+ VERSION = "1.0.0.pre"
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :carmen-rails do
+# # Task goes here
+# end
@@ -0,0 +1,93 @@
+require 'spec_helper'
+
+class CarmenViewHelperTest < MiniTest::Unit::TestCase
+ include ActionView::Helpers::FormOptionsHelper
+ include ActionDispatch::Assertions::SelectorAssertions
+
+ def setup
+ @object = OpenStruct.new
+ def @object.to_s; 'object'; end
+ end
+
+ def response_from_page
+ HTML::Document.new(@html).root
+ end
+
+ def test_basic_country_select
+ html = country_select(@object, :country_code)
+ expected = <<-HTML
+ <select id="object_country_code" name="object[country_code]">
+ <option value="OC">Oceania</option>
+ <option value="EU">Eurasia</option>
+ <option value="ES">Eastasia</option>
+ </select>
+ HTML
+
+ assert_equal_markup(expected, html)
+ end
+
+ def test_country_selected_value
+ @object.country_code = 'OC'
+ @html = country_select(@object, :country_code)
+ assert_select('option[selected="selected"][value="OC"]')
+ end
+
+ def test_priority_country_select
+ html = country_select(@object, :country_code, {priority: ['ES']})
+ expected = <<-HTML
+ <select id="object_country_code" name="object[country_code]">
+ <option value="ES">Eastasia</option>
+ <option disabled>-------------</option>
+ <option value="OC">Oceania</option>
+ <option value="EU">Eurasia</option>
+ <option value="ES">Eastasia</option>
+ </select>
+ HTML
+
+ assert_equal_markup(expected, html)
+ end
+
+ def test_basic_subregion_select
+ oceania = Carmen::Country.coded('OC')
+ expected = <<-HTML
+ <select id="object_subregion_code" name="object[subregion_code]"><option value="AO">Airstrip One</option></select>
+ HTML
+
+ html = subregion_select(@object, :subregion_code, oceania)
+
+ assert_equal_markup(expected, html)
+ end
+
+ def test_subregion_select_using_parent_code
+ expected = <<-HTML
+ <select id="object_subregion_code" name="object[subregion_code]"><option value="AO">Airstrip One</option></select>
+ HTML
+
+ html = subregion_select(@object, :subregion_code, 'OC')
+
+ assert_equal_markup(expected, html)
+ end
+
+ def test_subregion_select_using_parent_code_array
+ expected = <<-HTML
+ <select id="object_subregion_code" name="object[subregion_code]"><option value="LO">London</option></select>
+ HTML
+
+ html = subregion_select(@object, :subregion_code, ['OC', 'AO'])
+
+ assert_equal_markup(expected, html)
+ end
+
+ def test_subregion_selected_value
+ @object.subregion_code = 'AO'
+ oceania = Carmen::Country.coded('OC')
+
+ @html = subregion_select(@object, :subregion_code, oceania)
+
+ assert_select('option[selected="selected"][value="AO"]')
+ end
+
+ def method_missing(method, *args)
+ fail "method_missing #{method}"
+ end
+end
Oops, something went wrong.

0 comments on commit 1943e5e

Please sign in to comment.