br_railties 0.2.0 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/br_railties/municipalities_controller.rb +6 -0
- data/app/models/br_railties/federal_unit.rb +1 -0
- data/app/models/br_railties/municipality.rb +1 -0
- data/config/routes.rb +3 -2
- data/db/migrate/20190218210506_create_br_railties_federal_units.rb +12 -0
- data/db/migrate/20190221200607_create_br_railties_municipalities.rb +15 -0
- data/db/migrate/20190226015936_add_ibge_code_to_br_railties_federal_units.rb +7 -0
- data/db/migrate/20190227152340_add_ibge_code_to_br_railties_municipalities.rb +7 -0
- data/lib/br_railties.rb +5 -0
- data/lib/br_railties/engine.rb +2 -4
- data/lib/br_railties/ibge/import/federal_units.rb +43 -0
- data/lib/br_railties/ibge/import/municipalities.rb +51 -0
- data/lib/br_railties/version.rb +1 -1
- data/lib/tasks/br_railties.rake +17 -0
- metadata +26 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2017be09e01ef2b3bb00e68c07f6945f831f850c17c8c73490881f203ccb9488
|
4
|
+
data.tar.gz: a722daedd5253ff3fa1df8df092677b343d918736eb25e555ba2680f2321e949
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 912e69d99334e5a1ba36c5dec6c9ec5fd8d8d6704d66c09612b955dad334353ae3da72f02bb2fd0b161be0b452f9d0354a343a23bda15d0e9706b1462d689c81
|
7
|
+
data.tar.gz: 229b7b77e8ddca7c2c680c47fbc9d8217617087f13b3e905d9900ff2ff5018170d753e5ce2de12eb782b31cd497a9b08f96d9da87441de987ce536505974e9e5
|
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
module BrRailties
|
4
4
|
class MunicipalitiesController < ApplicationController
|
5
|
+
record_select per_page: 10,
|
6
|
+
search_on: [:name],
|
7
|
+
order_by: 'name ASC',
|
8
|
+
model: ::BrRailties::Municipality,
|
9
|
+
full_text_search: true
|
10
|
+
|
5
11
|
active_scaffold :"br_railties/municipality" do |conf|
|
6
12
|
conf.columns[:federal_unit].form_ui = :select
|
7
13
|
end
|
@@ -4,6 +4,7 @@ module BrRailties
|
|
4
4
|
class FederalUnit < ActiveRecord::Base
|
5
5
|
validates :acronym, presence: true, uniqueness: { case_sensitive: false }, length: { in: 2..2 }
|
6
6
|
validates :name, presence: true, uniqueness: { case_sensitive: false }
|
7
|
+
validates :ibge_code, presence: true, uniqueness: true, numericality: { greater_than: 0 }
|
7
8
|
|
8
9
|
def acronym=(value)
|
9
10
|
self[:acronym] = value.to_s.upcase
|
data/config/routes.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
BrRailties::Engine.routes.draw do
|
4
|
-
|
5
|
-
resources(:
|
4
|
+
concern :active_scaffold, ActiveScaffold::Routing::Basic.new(association: true)
|
5
|
+
resources(:federal_units, concerns: :active_scaffold)
|
6
|
+
resources(:municipalities, concerns: :active_scaffold) { record_select_routes }
|
6
7
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CreateBrRailtiesMunicipalities < ActiveRecord::Migration
|
4
|
+
def change
|
5
|
+
create_table :br_railties_municipalities do |t|
|
6
|
+
t.string :name
|
7
|
+
t.references :federal_unit, index: true
|
8
|
+
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
|
12
|
+
add_foreign_key :br_railties_municipalities, :br_railties_federal_units,
|
13
|
+
column: :federal_unit_id
|
14
|
+
end
|
15
|
+
end
|
data/lib/br_railties.rb
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'recordselect'
|
4
|
+
require 'active_scaffold'
|
5
|
+
|
3
6
|
require 'br_railties/engine'
|
7
|
+
require 'br_railties/ibge/import/federal_units'
|
8
|
+
require 'br_railties/ibge/import/municipalities'
|
4
9
|
|
5
10
|
module BrRailties
|
6
11
|
def self.table_name_prefix
|
data/lib/br_railties/engine.rb
CHANGED
@@ -5,10 +5,8 @@ module BrRailties
|
|
5
5
|
isolate_namespace BrRailties
|
6
6
|
|
7
7
|
initializer :append_migrations do |app|
|
8
|
-
|
9
|
-
config.paths['db/migrate']
|
10
|
-
app.config.paths['db/migrate'] << expanded_path
|
11
|
-
end
|
8
|
+
config.paths['db/migrate'].expanded.each do |expanded_path|
|
9
|
+
app.config.paths['db/migrate'] << expanded_path
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module BrRailties
|
6
|
+
module Ibge
|
7
|
+
module Import
|
8
|
+
class FederalUnits
|
9
|
+
SOURCE_URL = 'https://servicodados.ibge.gov.br/api/v1/localidades/estados'
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
run
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def run
|
18
|
+
source_data.each do |fu_source_data|
|
19
|
+
import(fu_source_data.except('regiao'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def source_data
|
24
|
+
JSON.parse(Net::HTTP.get(URI.parse(SOURCE_URL)))
|
25
|
+
end
|
26
|
+
|
27
|
+
def import(raw)
|
28
|
+
data = { acronym: raw['sigla'], name: raw['nome'], ibge_code: raw['id'] }
|
29
|
+
record = record_by_data(data)
|
30
|
+
record.attributes = data
|
31
|
+
::Rails.logger.info("Importing: #{record.attributes}")
|
32
|
+
record.save!
|
33
|
+
end
|
34
|
+
|
35
|
+
def record_by_data(data)
|
36
|
+
::BrRailties::FederalUnit.find_by(ibge_code: data[:ibge_code]) ||
|
37
|
+
::BrRailties::FederalUnit.find_by(acronym: data[:acronym]) ||
|
38
|
+
::BrRailties::FederalUnit.new
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module BrRailties
|
6
|
+
module Ibge
|
7
|
+
module Import
|
8
|
+
class Municipalities
|
9
|
+
SOURCE_URL = 'https://servicodados.ibge.gov.br/api/v1/localidades/municipios'
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
run
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def run
|
18
|
+
source_data.each do |municipality_source_data|
|
19
|
+
import(municipality_source_data)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def source_data
|
24
|
+
JSON.parse(Net::HTTP.get(URI.parse(SOURCE_URL)))
|
25
|
+
end
|
26
|
+
|
27
|
+
def import(raw)
|
28
|
+
data = { federal_unit: find_federal_unit(raw), name: raw['nome'], ibge_code: raw['id'] }
|
29
|
+
record = record_by_data(data)
|
30
|
+
record.attributes = data
|
31
|
+
::Rails.logger.info("Importing: #{record.attributes}")
|
32
|
+
record.save!
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_federal_unit(raw)
|
36
|
+
raw = raw['microrregiao']['mesorregiao']['UF']
|
37
|
+
::BrRailties::FederalUnit.find_by(ibge_code: raw['id']) ||
|
38
|
+
::BrRailties::FederalUnit.find_by(acronym: raw['sigla']) ||
|
39
|
+
raise("Federal unit not found (Raw data: #{raw})")
|
40
|
+
end
|
41
|
+
|
42
|
+
def record_by_data(data)
|
43
|
+
::BrRailties::Municipality.find_by(ibge_code: data[:ibge_code]) ||
|
44
|
+
::BrRailties::Municipality.find_by(name: data[:name],
|
45
|
+
federal_unit: data[:federal_unit]) ||
|
46
|
+
::BrRailties::Municipality.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/br_railties/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :br_railties do
|
4
|
+
namespace :ibge do
|
5
|
+
namespace :import do
|
6
|
+
desc 'Importa unidades federativas do cadastro do IBGE.'
|
7
|
+
task federal_units: :environment do |_t, _args|
|
8
|
+
::BrRailties::Ibge::Import::FederalUnits.new
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Importa municípios do cadastro do IBGE.'
|
12
|
+
task municipalities: :environment do |_t, _args|
|
13
|
+
::BrRailties::Ibge::Import::Municipalities.new
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: br_railties
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esquilo Azul Company
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_scaffold
|
@@ -30,14 +30,28 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 4.2.11.3
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 4.2.11.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: recordselect
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.10'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.10'
|
41
55
|
description:
|
42
56
|
email:
|
43
57
|
executables: []
|
@@ -54,9 +68,16 @@ files:
|
|
54
68
|
- app/models/br_railties/federal_unit.rb
|
55
69
|
- app/models/br_railties/municipality.rb
|
56
70
|
- config/routes.rb
|
71
|
+
- db/migrate/20190218210506_create_br_railties_federal_units.rb
|
72
|
+
- db/migrate/20190221200607_create_br_railties_municipalities.rb
|
73
|
+
- db/migrate/20190226015936_add_ibge_code_to_br_railties_federal_units.rb
|
74
|
+
- db/migrate/20190227152340_add_ibge_code_to_br_railties_municipalities.rb
|
57
75
|
- lib/br_railties.rb
|
58
76
|
- lib/br_railties/engine.rb
|
77
|
+
- lib/br_railties/ibge/import/federal_units.rb
|
78
|
+
- lib/br_railties/ibge/import/municipalities.rb
|
59
79
|
- lib/br_railties/version.rb
|
80
|
+
- lib/tasks/br_railties.rake
|
60
81
|
homepage: https://github.com/esquilo-azul/br_railties
|
61
82
|
licenses:
|
62
83
|
- MIT
|
@@ -77,8 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
98
|
- !ruby/object:Gem::Version
|
78
99
|
version: '0'
|
79
100
|
requirements: []
|
80
|
-
|
81
|
-
rubygems_version: 2.7.7
|
101
|
+
rubygems_version: 3.0.8
|
82
102
|
signing_key:
|
83
103
|
specification_version: 4
|
84
104
|
summary: Brazilian resources for Rails.
|