Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit 0ca3f57

Browse files
author
Kadu Diógenes
committed
initial commit
0 parents  commit 0ca3f57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1444
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.bundle/
2+
log/*.log
3+
pkg/
4+
spec/test_app/db/*.sqlite3
5+
spec/test_app/db/*.sqlite3-journal
6+
spec/test_app/log/*.log
7+
spec/test_app/tmp/
8+
spec/test_app/.sass-cache

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

Gemfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
source 'https://rubygems.org'
2+
3+
# Declare your gem's dependencies in cs_rails.gemspec.
4+
# Bundler will treat runtime dependencies like base dependencies, and
5+
# development dependencies will be added by default to the :development group.
6+
gemspec
7+
8+
# Declare any dependencies that are still in development here instead of in
9+
# your gemspec. These might include edge Rails or gems from your path or
10+
# Git. Remember to move these dependencies to your gemspec before releasing
11+
# your gem to rubygems.org.
12+
13+
# To use a debugger
14+
# gem 'byebug', group: [:development, :test]
15+

Gemfile.lock

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
PATH
2+
remote: .
3+
specs:
4+
cs-rails (0.0.1)
5+
coffee-rails
6+
countries
7+
rails (~> 4.2.5)
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
actionmailer (4.2.5.1)
13+
actionpack (= 4.2.5.1)
14+
actionview (= 4.2.5.1)
15+
activejob (= 4.2.5.1)
16+
mail (~> 2.5, >= 2.5.4)
17+
rails-dom-testing (~> 1.0, >= 1.0.5)
18+
actionpack (4.2.5.1)
19+
actionview (= 4.2.5.1)
20+
activesupport (= 4.2.5.1)
21+
rack (~> 1.6)
22+
rack-test (~> 0.6.2)
23+
rails-dom-testing (~> 1.0, >= 1.0.5)
24+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
25+
actionview (4.2.5.1)
26+
activesupport (= 4.2.5.1)
27+
builder (~> 3.1)
28+
erubis (~> 2.7.0)
29+
rails-dom-testing (~> 1.0, >= 1.0.5)
30+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
31+
activejob (4.2.5.1)
32+
activesupport (= 4.2.5.1)
33+
globalid (>= 0.3.0)
34+
activemodel (4.2.5.1)
35+
activesupport (= 4.2.5.1)
36+
builder (~> 3.1)
37+
activerecord (4.2.5.1)
38+
activemodel (= 4.2.5.1)
39+
activesupport (= 4.2.5.1)
40+
arel (~> 6.0)
41+
activesupport (4.2.5.1)
42+
i18n (~> 0.7)
43+
json (~> 1.7, >= 1.7.7)
44+
minitest (~> 5.1)
45+
thread_safe (~> 0.3, >= 0.3.4)
46+
tzinfo (~> 1.1)
47+
arel (6.0.3)
48+
builder (3.2.2)
49+
coffee-rails (4.1.1)
50+
coffee-script (>= 2.2.0)
51+
railties (>= 4.0.0, < 5.1.x)
52+
coffee-script (2.4.1)
53+
coffee-script-source
54+
execjs
55+
coffee-script-source (1.10.0)
56+
concurrent-ruby (1.0.0)
57+
countries (1.2.5)
58+
currencies (~> 0.4.2)
59+
i18n_data (~> 0.7.0)
60+
currencies (0.4.2)
61+
diff-lcs (1.2.5)
62+
erubis (2.7.0)
63+
execjs (2.6.0)
64+
globalid (0.3.6)
65+
activesupport (>= 4.1.0)
66+
haml (4.0.7)
67+
tilt
68+
haml-rails (0.9.0)
69+
actionpack (>= 4.0.1)
70+
activesupport (>= 4.0.1)
71+
haml (>= 4.0.6, < 5.0)
72+
html2haml (>= 1.0.1)
73+
railties (>= 4.0.1)
74+
html2haml (2.0.0)
75+
erubis (~> 2.7.0)
76+
haml (~> 4.0.0)
77+
nokogiri (~> 1.6.0)
78+
ruby_parser (~> 3.5)
79+
i18n (0.7.0)
80+
i18n_data (0.7.0)
81+
json (1.8.3)
82+
loofah (2.0.3)
83+
nokogiri (>= 1.5.9)
84+
mail (2.6.3)
85+
mime-types (>= 1.16, < 3)
86+
mime-types (2.99.1)
87+
mini_portile2 (2.0.0)
88+
minitest (5.8.4)
89+
nokogiri (1.6.7.2)
90+
mini_portile2 (~> 2.0.0.rc2)
91+
pg (0.18.4)
92+
rack (1.6.4)
93+
rack-test (0.6.3)
94+
rack (>= 1.0)
95+
rails (4.2.5.1)
96+
actionmailer (= 4.2.5.1)
97+
actionpack (= 4.2.5.1)
98+
actionview (= 4.2.5.1)
99+
activejob (= 4.2.5.1)
100+
activemodel (= 4.2.5.1)
101+
activerecord (= 4.2.5.1)
102+
activesupport (= 4.2.5.1)
103+
bundler (>= 1.3.0, < 2.0)
104+
railties (= 4.2.5.1)
105+
sprockets-rails
106+
rails-deprecated_sanitizer (1.0.3)
107+
activesupport (>= 4.2.0.alpha)
108+
rails-dom-testing (1.0.7)
109+
activesupport (>= 4.2.0.beta, < 5.0)
110+
nokogiri (~> 1.6.0)
111+
rails-deprecated_sanitizer (>= 1.0.1)
112+
rails-html-sanitizer (1.0.3)
113+
loofah (~> 2.0)
114+
railties (4.2.5.1)
115+
actionpack (= 4.2.5.1)
116+
activesupport (= 4.2.5.1)
117+
rake (>= 0.8.7)
118+
thor (>= 0.18.1, < 2.0)
119+
rake (10.5.0)
120+
rspec-core (3.4.3)
121+
rspec-support (~> 3.4.0)
122+
rspec-expectations (3.4.0)
123+
diff-lcs (>= 1.2.0, < 2.0)
124+
rspec-support (~> 3.4.0)
125+
rspec-mocks (3.4.1)
126+
diff-lcs (>= 1.2.0, < 2.0)
127+
rspec-support (~> 3.4.0)
128+
rspec-rails (3.4.2)
129+
actionpack (>= 3.0, < 4.3)
130+
activesupport (>= 3.0, < 4.3)
131+
railties (>= 3.0, < 4.3)
132+
rspec-core (~> 3.4.0)
133+
rspec-expectations (~> 3.4.0)
134+
rspec-mocks (~> 3.4.0)
135+
rspec-support (~> 3.4.0)
136+
rspec-support (3.4.1)
137+
ruby_parser (3.8.1)
138+
sexp_processor (~> 4.1)
139+
sexp_processor (4.7.0)
140+
sprockets (3.5.2)
141+
concurrent-ruby (~> 1.0)
142+
rack (> 1, < 3)
143+
sprockets-rails (3.0.3)
144+
actionpack (>= 4.0)
145+
activesupport (>= 4.0)
146+
sprockets (>= 3.0.0)
147+
thor (0.19.1)
148+
thread_safe (0.3.5)
149+
tilt (2.0.2)
150+
tzinfo (1.2.2)
151+
thread_safe (~> 0.1)
152+
153+
PLATFORMS
154+
ruby
155+
156+
DEPENDENCIES
157+
cs-rails!
158+
haml-rails
159+
pg
160+
rspec-rails
161+
162+
BUNDLED WITH
163+
1.11.2

MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright 2016 Kadu Diógenes
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# cs-rails
2+
3+
Sometime ago I used [carmen-rails](https://github.com/jim/carmen-rails) and liked the project, but it's not maintained anymore. I also felt that it was not very magical and some simple spells could make it more sexy!
4+
5+
## Instalation
6+
7+
```ruby
8+
gem 'cs-rails'
9+
```
10+
11+
## Usage
12+
13+
I only used with [Simple Form](https://github.com/plataformatec/simple_form) and didn't write any test, so no country_select_tag for now, you must have a FormBuilder to use it.
14+
15+
First, you have to mount cs-rails engine: `mount CsRails::Engine, at: '/cs'`
16+
17+
Add the country select with/without priority countries.
18+
19+
```ruby
20+
= f_address.input :country, priority: ['BR', 'AR', 'CL', 'PY', 'UY'], input_html: { data: { cs_rails: true } }
21+
```
22+
23+
You have to `#= require cs-rails/countries`. It will find any country input with data-cs-rails attribute and submit an ajax request to find the states for the selected country. If the states aren't find for the country, the state select is replaced by an input, so the user can write the state.
24+
25+
It will replace the contents for any select/input that have an id ending with "_state". If you want to control the select/input that must be replaced you use the `state-input-id` data attribute as following:
26+
27+
```ruby
28+
= f_address.input :country, priority: ['BR', 'AR', 'CL', 'PY', 'UY'], input_html: { data: { cs_rails: true, state_input_id: 'target_input_id' } }
29+
```
30+
31+
For now, it's all!
32+
33+
This project rocks and uses MIT-LICENSE.

Rakefile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
begin
2+
require 'bundler/setup'
3+
rescue LoadError
4+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5+
end
6+
7+
require 'rdoc/task'
8+
9+
RDoc::Task.new(:rdoc) do |rdoc|
10+
rdoc.rdoc_dir = 'rdoc'
11+
rdoc.title = 'CsRails'
12+
rdoc.options << '--line-numbers'
13+
rdoc.rdoc_files.include('README.rdoc')
14+
rdoc.rdoc_files.include('lib/**/*.rb')
15+
end
16+
17+
APP_RAKEFILE = File.expand_path("../spec/test_app/Rakefile", __FILE__)
18+
load 'rails/tasks/engine.rake'
19+
20+
21+
load 'rails/tasks/statistics.rake'
22+
23+
24+
25+
Bundler::GemHelper.install_tasks
26+

app/assets/images/cs-rails/.keep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// compiled file.
9+
//
10+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11+
// about supported directives.
12+
//
13+
//= require_tree .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$(document).on 'page:change', ->
2+
$('select[data-cs-rails]').change (event) ->
3+
$.get "<%= CsRails::Engine.routes.url_helpers.states_index_path %>",
4+
{
5+
country_code: $(@).val()
6+
state_input_id: $(@).data('state-input-id')
7+
}, null, 'script'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This is a manifest file that'll be compiled into application.css, which will include all the files
3+
* listed below.
4+
*
5+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7+
*
8+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
9+
* compiled file so the styles you add here take precedence over styles defined in any styles
10+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11+
* file per style scope.
12+
*
13+
*= require_tree .
14+
*= require_self
15+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Place all the styles related to the matching controller here.
3+
They will automatically be included in application.css.
4+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CsRails
2+
class ApplicationController < ActionController::Base
3+
end
4+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require_dependency "cs_rails/application_controller"
2+
3+
module CsRails
4+
class StatesController < ApplicationController
5+
def index
6+
@states = ::ISO3166::Country[params[:country_code]].states.map { |k, v| [v['name'], k] }
7+
@state_input_id = params[:state_input_id]
8+
respond_to { |format| format.js }
9+
end
10+
end
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CsRails
2+
module ApplicationHelper
3+
end
4+
end

app/helpers/cs_rails/states_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CsRails
2+
module StatesHelper
3+
end
4+
end
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
properties = (control, class_remove, class_add) ->
2+
{
3+
id: control.attr 'id'
4+
name: control.attr 'name'
5+
class: control.attr('class').replace(class_remove, class_add)
6+
}
7+
8+
states_length = "<%= @states.length %>"
9+
state_input_id = "<%= @state_input_id %>" || "_state"
10+
control = $("select[id$=#{state_input_id}], input[id$=#{state_input_id}]")
11+
if states_length > 0
12+
options = "<%=j options_for_select(@states) %>"
13+
if control.prop('tagName') == 'INPUT'
14+
control.replaceWith $('<select>', properties(control, 'string', 'select')).append(options)
15+
else
16+
control.empty().append options
17+
else
18+
control.replaceWith $('<input>', properties(control, 'select', 'string')) if control.prop('tagName') == 'SELECT'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CsRails</title>
5+
<%= stylesheet_link_tag "cs_rails/application", media: "all" %>
6+
<%= javascript_include_tag "cs_rails/application" %>
7+
<%= csrf_meta_tags %>
8+
</head>
9+
<body>
10+
11+
<%= yield %>
12+
13+
</body>
14+
</html>

bin/rails

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env ruby
2+
# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application.
3+
4+
ENGINE_ROOT = File.expand_path('../..', __FILE__)
5+
ENGINE_PATH = File.expand_path('../../lib/cs_rails/engine', __FILE__)
6+
7+
# Set up gems listed in the Gemfile.
8+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
9+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
10+
11+
require 'rails/all'
12+
require 'rails/engine/commands'

config/routes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CsRails::Engine.routes.draw do
2+
get 'states/index'
3+
end

0 commit comments

Comments
 (0)