countries_regions_and_cities_by_peterconsuegra 0.1.0 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa456d98f05cc99cbf04e237831e62d8ba5badfdc75e8d332a900b65bd377819
4
- data.tar.gz: 9828268ee276c5e95d3168f0d2b25cc33f3550f7880aa7926e450e1a7578e4a4
3
+ metadata.gz: c9dfb84c711a6dcd7b6e68cff31501be5bd3b31239b8d8c0d6c2b3dec8332ba3
4
+ data.tar.gz: 9afecebe3050f327fd1193e6250e8cdfcc1161f30a3ac3fab4cb0237ccc4f0ee
5
5
  SHA512:
6
- metadata.gz: 4635e5ba8b918dfaf52f0332ea3e54ec68b0996a029eed13d188975cdd0f37db68d3ce9ae0301624335aec67bf0d111892e879354c07b496a012553705f9c6ce
7
- data.tar.gz: 67fb740a42709944198d5b19394b8e3c193b1272b67398b2855ae2c26a5dce5ab8ff77a944ad1371af77f2785db2216ea8164f4f5a40db0ec33bc47ab678bc9a
6
+ metadata.gz: 1105e73d3502f0dc6a7fab871af689c0b0cf89b38b52fe85bc5b8b26882ded2bc95b13e39c44a60321fe50886ce8ac73c34d746d875b557261ef6daae6263347
7
+ data.tar.gz: 38c0aaf006d1b43a9e5ae41d42ccd8b29d1eb73cea4dc4eea2899dd01cacad2f59766fd95b2b969f2bc80b8b4dbe1a20a00d0faab8b164dbdb78ffff77facf28
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ countries_regions_and_cities_by_peterconsuegra (0.1.24)
5
+ carmen (~> 1.1.3)
6
+ city-state (~> 0.1.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.1.4.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ zeitwerk (~> 2.3)
17
+ ast (2.4.2)
18
+ carmen (1.1.3)
19
+ activesupport (>= 3.0.0)
20
+ city-state (0.1.0)
21
+ rubyzip (>= 1.1)
22
+ concurrent-ruby (1.1.9)
23
+ i18n (1.8.11)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.14.4)
26
+ parallel (1.21.0)
27
+ parser (3.0.3.2)
28
+ ast (~> 2.4.1)
29
+ rainbow (3.0.0)
30
+ rake (13.0.6)
31
+ regexp_parser (2.2.0)
32
+ rexml (3.2.5)
33
+ rubocop (1.23.0)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.0.0.0)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8, < 3.0)
38
+ rexml
39
+ rubocop-ast (>= 1.12.0, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 3.0)
42
+ rubocop-ast (1.14.0)
43
+ parser (>= 3.0.1.1)
44
+ ruby-progressbar (1.11.0)
45
+ rubyzip (2.3.2)
46
+ tzinfo (2.0.4)
47
+ concurrent-ruby (~> 1.0)
48
+ unicode-display_width (2.1.0)
49
+ zeitwerk (2.5.1)
50
+
51
+ PLATFORMS
52
+ arm64-darwin-20
53
+
54
+ DEPENDENCIES
55
+ countries_regions_and_cities_by_peterconsuegra!
56
+ rake (~> 13.0)
57
+ rubocop (~> 1.7)
58
+
59
+ BUNDLED WITH
60
+ 2.2.22
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # CountriesRegionsAndCitiesByPeterconsuegra
1
+ # Countries Regions And Cities By Peter Consuegra
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/countries_regions_and_cities_by_peterconsuegra`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ An agile way to implement Countries, Regions and Cities in your project, without the need to create additional tables
6
4
 
7
5
  ## Installation
8
6
 
@@ -16,23 +14,46 @@ And then execute:
16
14
 
17
15
  $ bundle install
18
16
 
19
- Or install it yourself as:
17
+ Run the rake command to generate the necessary files:
18
+
19
+ $ bundle exec rake 'install_countries_regions_and_cities_to_model[your_model_name]'
20
+
21
+
22
+ Rails
23
+ ===============
24
+
25
+ Add this code to the _form.html.erb partial of your_model_name form
26
+
27
+ ```
28
+
29
+ <div class="field" id="country_field">
30
+ <%= render partial: 'shared/peterconsuegra_country_select', locals: {model: form.object.class.name.downcase, label: "Country", selected: form.object.country} %>
31
+ </div>
32
+
33
+ <div class="field" id="region_field">
34
+ <%= render partial: 'shared/peterconsuegra_region_select', locals: {model: form.object.class.name.downcase, label: "Region / State", selected_country: form.object.country, selected: form.object.region} %>
35
+ </div>
36
+
37
+ <div class="field" id="city_field">
38
+ <%= render partial: 'shared/peterconsuegra_city_select', locals: {model: form.object.class.name.downcase, label: "City", selected_region: form.object.region, selected_country: form.object.country, selected: form.object.city} %>
39
+ </div>
40
+
41
+
42
+ ```
20
43
 
21
- $ gem install countries_regions_and_cities_by_peterconsuegra
44
+ Video Tutorial
45
+ ===============
22
46
 
23
- ## Usage
47
+ Watch this video to see how it works
24
48
 
25
- TODO: Write usage instructions here
49
+ [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/YOUTUBE_VIDEO_ID_HERE/0.jpg)](https://www.youtube.com/watch?v=YOUTUBE_VIDEO_ID_HERE)
26
50
 
27
- ## Development
28
51
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
52
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
53
 
33
54
  ## Contributing
34
55
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/countries_regions_and_cities_by_peterconsuegra. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/countries_regions_and_cities_by_peterconsuegra/blob/master/CODE_OF_CONDUCT.md).
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/peterconsuegra/countries_regions_and_cities. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/countries_regions_and_cities_by_peterconsuegra/blob/master/CODE_OF_CONDUCT.md).
36
57
 
37
58
  ## Code of Conduct
38
59
 
data/Rakefile CHANGED
@@ -6,3 +6,9 @@ require "rubocop/rake_task"
6
6
  RuboCop::RakeTask.new
7
7
 
8
8
  task default: :rubocop
9
+
10
+ require 'countries_regions_and_cities_by_peterconsuegra'
11
+
12
+ spec = Gem::Specification.find_by_name 'my_gem'
13
+ rakefile = "#{spec.gem_dir}/lib/tasks/hello_pete.rake"
14
+ load rakefile
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
 
35
35
  spec.add_dependency("carmen", "~> 1.1.3")
36
36
  spec.add_dependency("city-state","~> 0.1.0")
37
+ spec.add_dependency("colorize","~> 0.8.0")
37
38
 
38
39
  # For more information and examples about making a new gem, checkout our
39
40
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CountriesRegionsAndCitiesByPeterconsuegra
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -2,12 +2,29 @@
2
2
 
3
3
  require_relative "countries_regions_and_cities_by_peterconsuegra/version"
4
4
 
5
+ require 'carmen'
6
+ require 'city-state'
7
+
5
8
  module CountriesRegionsAndCitiesByPeterconsuegra
9
+
10
+ # require 'countries_regions_and_cities_by_peterconsuegra/railtie' if defined?(Rails)
11
+
6
12
  class Error < StandardError; end
7
13
  # Your code goes here...
8
14
 
9
- def self.hi_pete
10
- puts "Hi pete!"
15
+ def self.get_all_countries
16
+ return Carmen::Country::all
17
+ end
18
+
19
+ def self.get_regions(selected_country)
20
+ country = Carmen::Country.coded(selected_country)
21
+ return country.subregions if country
22
+ end
23
+
24
+ def self.get_cities(selected_region,selected_country)
25
+ CS.cities(selected_region,selected_country)
11
26
  end
12
27
 
13
28
  end
29
+
30
+ require_relative "railtie" if defined?(Rails::Railtie)
@@ -0,0 +1,32 @@
1
+ require 'colorize'
2
+
3
+
4
+ module PeterConsuegraRecipes
5
+
6
+ def self.move_templates(src_folder,dest_folder,files)
7
+ files.each do |file_name|
8
+ #puts "src_folder: #{src_folder} dest_folder: #{dest_folder}".blue
9
+ FileUtils.cp(src_folder+file_name,dest_folder+file_name)
10
+ puts "file copied to: #{dest_folder+file_name}".green
11
+ end
12
+ end
13
+
14
+ def self.append_before_last_appearance_of(string,code,file)
15
+ File.open(file, 'r+') do |file|
16
+ lines = file.each_line.to_a
17
+ length = lines.length()
18
+
19
+ lines.reverse.each_with_index do |val, index|
20
+ if val.include?(string)
21
+ aux = lines[length-(index+1)]
22
+ lines[length-(index+1)] = "#{code} \n"
23
+ lines.append(aux)
24
+ file.rewind
25
+ file.write(lines.join)
26
+ break
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ end
data/lib/railtie.rb ADDED
@@ -0,0 +1,13 @@
1
+ #require 'countries_regions_and_cities_by_peterconsuegra'
2
+ #require 'rails'
3
+
4
+ module CountriesRegionsAndCitiesByPeterconsuegra
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :countries_regions_and_cities_by_peterconsuegra
7
+
8
+ rake_tasks do
9
+ path = File.expand_path(__dir__)
10
+ Dir.glob("#{path}/tasks/*.rake").each { |f| load f }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../peterconsuegra_recipes'
2
+ require 'fileutils'
3
+ #bundle exec rake 'install_countries_regions_and_cities_to_model[place]'
4
+
5
+ desc 'install countries_regions_and_cities required files'
6
+ task :install_countries_regions_and_cities_to_model, [:model] do |t, args|
7
+
8
+ rails_app_folder = Dir.pwd
9
+ gem_folder = File.expand_path('../../../.', __FILE__)
10
+
11
+ puts "rails_app_folder: #{rails_app_folder}"
12
+ puts "gem_folder: #{gem_folder}"
13
+
14
+ #adding partials
15
+ dest_folder="#{rails_app_folder}/app/views/shared/"
16
+ src_folder="#{gem_folder}/templates/"
17
+ files=["_peterconsuegra_city_select.html.erb","_peterconsuegra_country_select.html.erb","_peterconsuegra_region_select.html.erb"]
18
+ FileUtils.mkdir_p dest_folder
19
+ PeterConsuegraRecipes::move_templates(src_folder,dest_folder,files)
20
+
21
+ #adding controller
22
+ dest_folder="#{rails_app_folder}/app/controllers/"
23
+ files=["peterconsuegra_countries_regions_and_cities_controller.rb"]
24
+ PeterConsuegraRecipes::move_templates(src_folder,dest_folder,files)
25
+
26
+ #adding routes to config.rb
27
+ file="#{rails_app_folder}/config/routes.rb"
28
+ PeterConsuegraRecipes::append_before_last_appearance_of("end",'get "get_regions", to: "peterconsuegra_countries_regions_and_cities#get_regions"',file)
29
+ PeterConsuegraRecipes::append_before_last_appearance_of("end",'get "get_cities", to: "peterconsuegra_countries_regions_and_cities#get_cities"',file)
30
+
31
+ #adding migration to model
32
+ model = args[:model]
33
+ model = model.capitalize
34
+
35
+ `rails generate migration AddFieldsTo#{model} country:string region:string city:string`
36
+ sleep 2
37
+ `rake db:migrate`
38
+
39
+
40
+ end
@@ -0,0 +1,13 @@
1
+ <div id="city_code_wrapper">
2
+ <% selected ||= selected %>
3
+ <% cities = CountriesRegionsAndCitiesByPeterconsuegra::get_cities(selected_region,selected_country)%>
4
+
5
+ <%
6
+ #byebug
7
+ %>
8
+
9
+ <% if cities %>
10
+ <label><%=label%></label><br />
11
+ <%= select_tag "#{model}[city]", options_for_select(cities, selected), include_blank: true %>
12
+ <% end %>
13
+ </div>
@@ -0,0 +1,39 @@
1
+ <% country_options_array = CountriesRegionsAndCitiesByPeterconsuegra::get_all_countries %>
2
+
3
+ <div id="country_code_wrapper">
4
+ <label><%=label%></label><br />
5
+ <%= select_tag "#{model}[country]", options_for_select(country_options_array.sort_by{|c| c.name}.map{|c| [c.name, c.code]}, selected), include_blank: true %>
6
+ </div>
7
+
8
+ <script>
9
+
10
+ function city_select_logic(){
11
+
12
+ $('select#place_region').change(function(event) {
13
+
14
+ selected_country = $('select#<%=model%>_country').val();
15
+ selected_region = $(this).val();
16
+ locale = $('.locale').data('locale')
17
+ url = "/get_cities?selected_country=" + selected_country + "&selected_region=" + selected_region + "&model=<%=model%>"
18
+ $('#city_field').load(url)
19
+
20
+ })
21
+ }
22
+
23
+ $(function() {
24
+
25
+ $('select#place_country').change(function(event) {
26
+
27
+ selected_country = $(this).val();
28
+ $('select#<%=model%>_city').val("");
29
+ locale = $('.locale').data('locale')
30
+ url = "/get_regions?selected_country=" + selected_country + "&model=<%=model%>"
31
+ $('#region_field').load(url, function() {
32
+ city_select_logic();
33
+ });
34
+ })
35
+
36
+ city_select_logic();
37
+
38
+ })
39
+ </script>
@@ -0,0 +1,19 @@
1
+ <% regions = CountriesRegionsAndCitiesByPeterconsuegra::get_regions(selected_country) %>
2
+ <% selected ||= selected %>
3
+
4
+ <div id="region_code_wrapper">
5
+ <% if regions %>
6
+ <label><%=label%></label><br />
7
+ <%
8
+ # byebug
9
+ %>
10
+ <%= select_tag "#{model}[region]", options_for_select(regions.sort_by{|c| c.name}.map{|c| [c.name, c.code]}, selected), include_blank: true %>
11
+ <% end %>
12
+ </div>
13
+
14
+
15
+ <script>
16
+
17
+
18
+
19
+ </script>
@@ -0,0 +1,16 @@
1
+ class PeterconsuegraCountriesRegionsAndCitiesController < ApplicationController
2
+
3
+ def get_regions
4
+ selected_country ||= params[:selected_country]
5
+ model ||= params[:model]
6
+ render partial: 'shared/peterconsuegra_region_select', locals: {model: model, label: "Region / State", selected_country: selected_country}
7
+ end
8
+
9
+ def get_cities
10
+ selected_country ||= params[:selected_country]
11
+ selected_region ||= params[:selected_region]
12
+ model ||= params[:model]
13
+ render partial: 'shared/peterconsuegra_city_select', locals: {model: model, label: "Ciudad", selected_region: selected_region, selected_country: selected_country}
14
+ end
15
+
16
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: countries_regions_and_cities_by_peterconsuegra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pedro Consuegra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-08 00:00:00.000000000 Z
11
+ date: 2021-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carmen
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.8.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.8.0
41
55
  description: An agile way to implement Countries, Regions and Cities in your project,
42
56
  without the need to create additional tables
43
57
  email:
@@ -51,6 +65,7 @@ files:
51
65
  - CHANGELOG.md
52
66
  - CODE_OF_CONDUCT.md
53
67
  - Gemfile
68
+ - Gemfile.lock
54
69
  - README.md
55
70
  - Rakefile
56
71
  - bin/console
@@ -58,6 +73,13 @@ files:
58
73
  - countries_regions_and_cities_by_peterconsuegra.gemspec
59
74
  - lib/countries_regions_and_cities_by_peterconsuegra.rb
60
75
  - lib/countries_regions_and_cities_by_peterconsuegra/version.rb
76
+ - lib/peterconsuegra_recipes.rb
77
+ - lib/railtie.rb
78
+ - lib/tasks/install_countries_regions_and_cities.rake
79
+ - templates/_peterconsuegra_city_select.html.erb
80
+ - templates/_peterconsuegra_country_select.html.erb
81
+ - templates/_peterconsuegra_region_select.html.erb
82
+ - templates/peterconsuegra_countries_regions_and_cities_controller.rb
61
83
  homepage: https://rubygems.org/gems/countries_regions_and_cities_by_peterconsuegra
62
84
  licenses: []
63
85
  metadata: