countries_regions_and_cities_by_peterconsuegra 0.1.0 → 0.2.0

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: 84156e1172a494938a073aec3289447a229c4311821f07bff79200196bffe295
4
+ data.tar.gz: 7c0d086b692428ac0ec780c511954356cc15503befd98508760133e028488dbf
5
5
  SHA512:
6
- metadata.gz: 4635e5ba8b918dfaf52f0332ea3e54ec68b0996a029eed13d188975cdd0f37db68d3ce9ae0301624335aec67bf0d111892e879354c07b496a012553705f9c6ce
7
- data.tar.gz: 67fb740a42709944198d5b19394b8e3c193b1272b67398b2855ae2c26a5dce5ab8ff77a944ad1371af77f2785db2216ea8164f4f5a40db0ec33bc47ab678bc9a
6
+ metadata.gz: ceae9ecfd914b9db3276d617f3f1dd3f34e3c8d91447135f064d96a5510809489532ab003256caed7990746be41f0de9ee1e425d0f9641f1463d899fb59388c4
7
+ data.tar.gz: 7a2f166865b8c3fbd520edb29af45d370c06c3e39f5f5dc86a249703feafd22a7b9e7835f92b7bf6d8b927487bc6f4efdf1703a820371d671e385e266495b861
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/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.0"
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,30 @@
1
+ require 'colorize'
2
+
3
+ module PeterConsuegraRecipes
4
+
5
+ def self.move_templates(src_folder,dest_folder,files)
6
+ files.each do |file_name|
7
+ FileUtils.cp(src_folder+file_name,dest_folder+file_name)
8
+ puts "file copied to: #{dest_folder+file_name}".green
9
+ end
10
+ end
11
+
12
+ def self.append_before_last_appearance_of(string,code,file)
13
+ File.open(file, 'r+') do |file|
14
+ lines = file.each_line.to_a
15
+ length = lines.length()
16
+
17
+ lines.reverse.each_with_index do |val, index|
18
+ if val.include?(string)
19
+ aux = lines[length-(index+1)]
20
+ lines[length-(index+1)] = "#{code} \n"
21
+ lines.append(aux)
22
+ file.rewind
23
+ file.write(lines.join)
24
+ break
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ 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,37 @@
1
+ require_relative '../peterconsuegra_recipes'
2
+
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
+ #adding partials
12
+ dest_folder="#{rails_app_folder}/app/views/shared/"
13
+ src_folder="#{gem_folder}/templates/"
14
+ files=["_peterconsuegra_city_select.html.erb","_peterconsuegra_country_select.html.erb","_peterconsuegra_region_select.html.erb"]
15
+ PeterConsuegraRecipes::move_templates(src_folder,dest_folder,files)
16
+
17
+ #adding controller
18
+ dest_folder="#{rails_app_folder}/app/controllers/"
19
+ files=["peterconsuegra_countries_regions_and_cities_controller.rb"]
20
+ PeterConsuegraRecipes::move_templates(src_folder,dest_folder,files)
21
+
22
+ #adding routes to config.rb
23
+ file="#{rails_app_folder}/config/routes.rb"
24
+ PeterConsuegraRecipes::append_before_last_appearance_of("end",'get "get_regions", to: "peterconsuegra_countries_regions_and_cities#get_regions"',file)
25
+ PeterConsuegraRecipes::append_before_last_appearance_of("end",'get "get_cities", to: "peterconsuegra_countries_regions_and_cities#get_cities"',file)
26
+
27
+ #adding migration to model
28
+ puts "table name:"
29
+ model = args[:model]
30
+ model = model.capitalize
31
+
32
+ `rails generate migration AddFieldsTo#{model} country:string region:string city:string`
33
+ sleep 2
34
+ `rake db:migrate`
35
+
36
+
37
+ 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.0
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-09 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: