countries_regions_and_cities_by_pete 0.2.0 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +59 -29
- data/countries_regions_and_cities_by_pete-0.3.0.gem +0 -0
- data/lib/countries_regions_and_cities_by_pete/version.rb +1 -1
- data/lib/peterconsuegra_recipes.rb +27 -3
- data/lib/tasks/install_countries_regions_and_cities_by_pete.rake +28 -43
- metadata +3 -4
- data/countries_regions_and_cities_by_pete-0.1.26.gem +0 -0
- data/countries_regions_and_cities_by_pete-0.1.7.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f84c7164171db6a80e8423758362b8d48c4d2ef66d6daf66274246321d94887
|
4
|
+
data.tar.gz: 649ddf959ae18e014be9457ad511f153d8705282ae74290c343a9fc73cb89190
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20486e3797b78630fb190d1d0c3ad2b39e447689a68576bfaba2289c48b1196164100a093f4202e7971dad690baa32358f9753aed2cf9727d66cf7701aadb403
|
7
|
+
data.tar.gz: b7668fbb91d30cfb5506089c7d28ea7ec66dae1cb6b56d7e53ba396e11374fb46b8a600c9861b8995ddd52408c4d42ad00a850f4a9182fe0728148b733854b09
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,71 +1,101 @@
|
|
1
|
-
# Countries Regions
|
1
|
+
# Ruby On Rails Countries, Regions and Cities by Pete
|
2
2
|
|
3
|
-
An agile way to implement Countries, Regions and Cities in
|
3
|
+
An agile way to implement Countries, Regions, and Cities in a Ruby On Rails scaffold without the need to create additional tables
|
4
|
+
|
5
|
+
## Support
|
6
|
+
|
7
|
+
Ruby On Rails: 5, 6, 7
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
8
12
|
|
9
13
|
```ruby
|
10
|
-
gem '
|
14
|
+
gem 'countries_regions_and_cities_by_pete'
|
11
15
|
```
|
12
16
|
|
13
17
|
And then execute:
|
14
18
|
|
15
19
|
$ bundle install
|
16
20
|
|
17
|
-
Add the jquery.min.js file to the layout you are using: /app/views/layouts/application.html.erb
|
18
21
|
|
19
|
-
|
20
|
-
|
22
|
+
## Usage
|
23
|
+
Let's create a example for a Place scaffold:
|
24
|
+
|
25
|
+
1. Let's create a new rails app:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
rails new myapp
|
21
29
|
```
|
22
30
|
|
23
|
-
|
31
|
+
2. Create the database:
|
24
32
|
|
25
|
-
|
33
|
+
```ruby
|
34
|
+
bundle exec rake db:create
|
35
|
+
```
|
26
36
|
|
27
|
-
|
37
|
+
3. Go to the application directory in Terminal and run the following command to create a scaffold for the Place model:
|
28
38
|
|
29
39
|
```ruby
|
30
|
-
|
31
|
-
def hotel_params
|
32
|
-
params.require(:hotel).permit(:name, :description, :address, :country, :region, :city)
|
33
|
-
end
|
40
|
+
bundle exec rails g scaffold Place name:string description:text
|
34
41
|
```
|
35
42
|
|
36
|
-
Add this
|
43
|
+
4. Add this line to your application's Gemfile:
|
37
44
|
|
45
|
+
```ruby
|
46
|
+
gem 'countries_regions_and_cities_by_pete'
|
38
47
|
```
|
39
48
|
|
40
|
-
|
41
|
-
<%= render partial: 'shared/peterconsuegra_country_select', locals: {model: form.object.class.name.downcase, label: "Country", selected: form.object.country} %>
|
42
|
-
</div>
|
49
|
+
5. Go to your application's directory in Terminal and run:
|
43
50
|
|
44
|
-
|
45
|
-
|
46
|
-
|
51
|
+
```ruby
|
52
|
+
bundle install
|
53
|
+
```
|
47
54
|
|
48
|
-
|
49
|
-
<%= 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} %>
|
50
|
-
</div>
|
55
|
+
6. Go to the application directory in Terminal and run the following command to install the necessary code and files from the gem:
|
51
56
|
|
57
|
+
```ruby
|
58
|
+
bundle exec rake 'install_countries_regions_and_cities_by_pete[Place]'
|
59
|
+
```
|
52
60
|
|
61
|
+
7. Add jQuery to your layout file: /app/views/layouts/application.html.erb
|
62
|
+
|
63
|
+
```html
|
64
|
+
<script src='/countries_regions_and_cities_by_pete/jquery-3.6.0.min.js'></script>
|
53
65
|
```
|
54
66
|
|
55
|
-
|
56
|
-
===============
|
67
|
+
8. Paste this code to your _form.html.erb file: /app/views/places/_form.html.erb
|
57
68
|
|
58
|
-
|
69
|
+
```ruby
|
70
|
+
<div class="field" id="country_field">
|
71
|
+
<%= render partial: "shared/country_select_by_pete", locals: {model: form.object.class.name, label: "Country", selected: form.object.country} %>
|
72
|
+
</div>
|
73
|
+
<div class="field" id="region_field">
|
74
|
+
<%= render partial: "shared/region_select_by_pete", locals: {model: form.object.class.name, label: "Region / State", selected_country: form.object.country, selected: form.object.region} %>
|
75
|
+
</div>
|
76
|
+
<div class="field" id="city_field">
|
77
|
+
<%= render partial: "shared/city_select_by_pete", locals: {model: form.object.class.name, label: "City", selected_region: form.object.region, selected_country: form.object.country, selected: form.object.city} %>
|
78
|
+
</div>
|
79
|
+
```
|
80
|
+
9. Allow parameters (country,region and city) in your controller: /app/controllers/places_controller.rb
|
59
81
|
|
60
|
-
|
82
|
+
```ruby
|
83
|
+
def place_params
|
84
|
+
params.require(:place).permit(:name, :description, :address, :country, :region, :city)
|
85
|
+
end
|
86
|
+
```
|
61
87
|
|
88
|
+
Video Tutorial
|
89
|
+
===============
|
62
90
|
|
91
|
+
Watch this video to see how it works
|
63
92
|
|
93
|
+
[![IMAGE ALT TEXT HERE](https://ozonegroup.co/countries_regions_and_cities.png)](https://www.youtube.com/watch?v=mSwfFtYE8HE)
|
64
94
|
|
65
95
|
## Contributing
|
66
96
|
|
67
|
-
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/
|
97
|
+
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/peterconsuegra/countries_regions_and_cities_by_peterconsuegra/blob/master/CODE_OF_CONDUCT.md).
|
68
98
|
|
69
99
|
## Code of Conduct
|
70
100
|
|
71
|
-
Everyone interacting in the CountriesRegionsAndCitiesByPeterconsuegra project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
101
|
+
Everyone interacting in the CountriesRegionsAndCitiesByPeterconsuegra project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/peterconsuegra/countries_regions_and_cities_by_peterconsuegra/blob/master/CODE_OF_CONDUCT.md).
|
Binary file
|
@@ -1,16 +1,40 @@
|
|
1
1
|
require 'colorize'
|
2
|
-
|
2
|
+
require 'fileutils'
|
3
|
+
require "tty-file"
|
3
4
|
|
4
5
|
module PeterConsuegraRecipes
|
5
6
|
|
6
7
|
def self.move_templates(src_folder,dest_folder,files)
|
8
|
+
FileUtils.mkdir_p dest_folder
|
7
9
|
files.each do |file_name|
|
8
|
-
#puts "src_folder: #{src_folder} dest_folder: #{dest_folder}".blue
|
9
10
|
FileUtils.cp(src_folder+file_name,dest_folder+file_name)
|
10
11
|
puts "file copied to: #{dest_folder+file_name}".green
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
15
|
+
def self.move_template(src_folder,dest_folder,file)
|
16
|
+
FileUtils.mkdir_p dest_folder
|
17
|
+
FileUtils.cp src_folder+file,dest_folder+file
|
18
|
+
puts "File copied to: #{dest_folder+file}".green
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.add_route(base_route,action_name,method)
|
22
|
+
rails_app_folder = Dir.pwd
|
23
|
+
route_file="#{rails_app_folder}/config/routes.rb"
|
24
|
+
route_code = "#{method} '/#{base_route}_#{action_name}', to: '#{base_route}##{action_name}'"
|
25
|
+
PeterConsuegraRecipes::append_before_last_appearance_of("end",route_code,route_file)
|
26
|
+
puts "Adding route: #{route_code}".blue
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.add_concern_to_controller(code,controller_file,controller_class)
|
30
|
+
rails_app_folder = Dir.pwd
|
31
|
+
file="#{rails_app_folder}/app/controllers/#{controller_file}"
|
32
|
+
#puts "class #{controller_class} < ApplicationController"
|
33
|
+
#code="include FilesByPete\n"
|
34
|
+
TTY::File.inject_into_file file, code, after: "class #{controller_class} < ApplicationController\n"
|
35
|
+
puts "Adding concern to controller #{file}: #{code}".blue
|
36
|
+
end
|
37
|
+
|
14
38
|
def self.append_before_last_appearance_of(string,code,file)
|
15
39
|
File.open(file, 'r+') do |file|
|
16
40
|
lines = file.each_line.to_a
|
@@ -37,7 +61,7 @@ module PeterConsuegraRecipes
|
|
37
61
|
hash["controller_file"] = hash["table_name"]+"_controller.rb"
|
38
62
|
hash["controller_class"] = hash["table_name"].titleize.gsub(' ','')+"Controller"
|
39
63
|
hash["base_route"] = hash["table_name"]
|
40
|
-
puts hash
|
64
|
+
#puts hash
|
41
65
|
return hash
|
42
66
|
end
|
43
67
|
|
@@ -1,60 +1,45 @@
|
|
1
1
|
require_relative '../peterconsuegra_recipes'
|
2
|
-
require '
|
3
|
-
require "tty-file"
|
2
|
+
require 'colorize'
|
4
3
|
|
5
|
-
#bundle exec rake 'install_countries_regions_and_cities_by_pete[
|
6
|
-
|
7
|
-
Dir.glob("#{Rails.root}/app/models/*.rb").each { |file| require file }
|
4
|
+
#bundle exec rake 'install_countries_regions_and_cities_by_pete[model]'
|
8
5
|
|
9
6
|
desc 'install countries_regions_and_cities required files'
|
10
7
|
task :install_countries_regions_and_cities_by_pete, [:model] do |t, args|
|
11
|
-
|
8
|
+
|
9
|
+
#Require all models
|
10
|
+
Dir.glob("#{Rails.root}/app/models/*.rb").each { |file| require file }
|
11
|
+
|
12
12
|
#Base routes
|
13
13
|
rails_app_folder = Dir.pwd
|
14
14
|
gem_folder = File.expand_path('../../../.', __FILE__)
|
15
|
+
src_folder="#{gem_folder}/templates/"
|
15
16
|
|
16
17
|
#Get scaffold vars
|
17
18
|
hash = PeterConsuegraRecipes::get_scaffold_vars(args[:model])
|
18
19
|
|
19
|
-
#Adding partials
|
20
|
-
|
21
|
-
src_folder="#{gem_folder}/templates/"
|
22
|
-
files=["_city_select_by_pete.html.erb","_country_select_by_pete.html.erb","_region_select_by_pete.html.erb"]
|
23
|
-
FileUtils.mkdir_p dest_folder
|
24
|
-
PeterConsuegraRecipes::move_templates(src_folder,dest_folder,files)
|
25
|
-
|
26
|
-
#Adding concern
|
27
|
-
dest_folder="#{rails_app_folder}/app/controllers/concerns/"
|
28
|
-
file="globalization_by_pete.rb"
|
29
|
-
FileUtils.mkdir_p dest_folder
|
30
|
-
FileUtils.cp src_folder+file,dest_folder+file
|
31
|
-
puts "File copied to: #{dest_folder+file}".green
|
32
|
-
|
33
|
-
#Copy jquery-3.6.0.min.js file
|
34
|
-
dest_folder="#{rails_app_folder}/public/countries_regions_and_cities_by_pete/"
|
35
|
-
file="jquery-3.6.0.min.js"
|
36
|
-
FileUtils.mkdir_p dest_folder
|
37
|
-
FileUtils.cp src_folder+file,dest_folder+file
|
38
|
-
puts "File copied to: #{dest_folder+file}".green
|
39
|
-
|
40
|
-
#Adding route to routes.rb
|
41
|
-
file="#{rails_app_folder}/config/routes.rb"
|
42
|
-
route1 = "get '/#{hash['base_route']}_get_regions', to: '#{hash['base_route']}#get_regions'"
|
43
|
-
route2 = "get '/#{hash['base_route']}_get_cities', to: '#{hash['base_route']}#get_cities'"
|
44
|
-
PeterConsuegraRecipes::append_before_last_appearance_of("end",route1,file)
|
45
|
-
PeterConsuegraRecipes::append_before_last_appearance_of("end",route2,file)
|
46
|
-
puts "Adding route: #{route1}".blue
|
47
|
-
puts "Adding route: #{route2}".blue
|
20
|
+
#Adding partials
|
21
|
+
PeterConsuegraRecipes::move_templates(src_folder,"#{rails_app_folder}/app/views/shared/",["_city_select_by_pete.html.erb","_country_select_by_pete.html.erb","_region_select_by_pete.html.erb"])
|
48
22
|
|
49
|
-
#Adding
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
23
|
+
#Adding concern file
|
24
|
+
PeterConsuegraRecipes::move_template(src_folder,"#{rails_app_folder}/app/controllers/concerns/","globalization_by_pete.rb")
|
25
|
+
|
26
|
+
#Adding js asset
|
27
|
+
PeterConsuegraRecipes::move_template(src_folder,"#{rails_app_folder}/public/countries_regions_and_cities_by_pete/","jquery-3.6.0.min.js")
|
28
|
+
|
29
|
+
#Adding route
|
30
|
+
PeterConsuegraRecipes::add_route(hash['base_route'],"get_regions","get")
|
31
|
+
PeterConsuegraRecipes::add_route(hash['base_route'],"get_cities","get")
|
32
|
+
|
33
|
+
#Adding concern to controller
|
34
|
+
PeterConsuegraRecipes::add_concern_to_controller("include GlobalizationByPete\n",hash['controller_file'],hash['controller_class'])
|
55
35
|
|
36
|
+
#Adding country, region and city fields
|
37
|
+
puts "Running command:".blue
|
38
|
+
puts "rails generate migration AddCountriesRegionsAndCitiesByPeteFieldsTo#{hash['model_class']} country:string region:string city:string".green
|
56
39
|
`rails generate migration AddFieldsTo#{hash['model_class']} country:string region:string city:string`
|
57
40
|
sleep 2
|
41
|
+
puts "Running command:".blue
|
42
|
+
puts "rake db:migrate".green
|
58
43
|
`rake db:migrate`
|
59
44
|
|
60
45
|
#Print necessary code
|
@@ -76,7 +61,7 @@ task :install_countries_regions_and_cities_by_pete, [:model] do |t, args|
|
|
76
61
|
puts ' <div class="field" id="city_field">
|
77
62
|
<%= render partial: "shared/city_select_by_pete", locals: {model: form.object.class.name, label: "City", selected_region: form.object.region, selected_country: form.object.country, selected: form.object.city} %>
|
78
63
|
</div>'.red
|
79
|
-
puts "Allow
|
80
|
-
puts "params.require(:#{hash['model']}).permit(:city, :region, :country)".red
|
64
|
+
puts "Allow :city, region and :country params in your controller #{hash['controller_file']}:".red
|
65
|
+
puts "params.require(:#{hash['model'].downcase}).permit(:city, :region, :country)".red
|
81
66
|
|
82
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: countries_regions_and_cities_by_pete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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:
|
11
|
+
date: 2022-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: carmen
|
@@ -84,8 +84,7 @@ files:
|
|
84
84
|
- Rakefile
|
85
85
|
- bin/console
|
86
86
|
- bin/setup
|
87
|
-
- countries_regions_and_cities_by_pete-0.
|
88
|
-
- countries_regions_and_cities_by_pete-0.1.7.gem
|
87
|
+
- countries_regions_and_cities_by_pete-0.3.0.gem
|
89
88
|
- countries_regions_and_cities_by_pete.gemspec
|
90
89
|
- countries_regions_and_cities_by_peterconsuegra-0.2.5.gem
|
91
90
|
- lib/countries_regions_and_cities_by_pete.rb
|
Binary file
|
Binary file
|