br_railties 0.1.0 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b583901687c0ec84be9f7c61822c1e1f14c0ef8e3100b9e84fd763524d52fbc
4
- data.tar.gz: b90b9c760edd0570e43a02c7172c9d1951f1e1dbca928dfd2f7642af9df6a6a1
3
+ metadata.gz: 5be5cd3559547e02e00ab068bf3083c0ac8cb6f950bcb63a8ada2b9d5492e11f
4
+ data.tar.gz: 5ea9fa40f1fe8c98bf355609a661c2976dbbf1cb157f6662d082efe91b364c41
5
5
  SHA512:
6
- metadata.gz: bdd2871248733bc819bac56e5c96663717810e76065c9b0ed4de005f1d5e68edb41727a919d1596e4fe12bea1e8c783ab6c48b24942743206f04129759bb9c8d
7
- data.tar.gz: c1a65cc3084a24b31327e8aa2626e204736d6b750ceff1633f97ed53ddfe0e704a73981299131718c5c3cbbf637d22facb8f18baa333de559100f0d597ceb26f
6
+ metadata.gz: f2bb6236338094b192b307598b8ef0cba15cfb59ed05470f86372cd3d69a23970fab28ba5e59c6eeaec8d451a0ec2c9367e2da8b327a31c464d2ae9b69dde3ca
7
+ data.tar.gz: adef2f1e076f5c2ffbe73f718dbf26ac13a9b9b1f5d3bdae668b888aa9ff5981d98c9329ec3133932ba34d74362f2c415707631218cd9d3bf95530a18dd2c5fd
@@ -1,4 +1,8 @@
1
- class BrRailties::FederalUnitsController < ApplicationController
2
- active_scaffold :"br_railties/federal_unit" do |conf|
1
+ # frozen_string_literal: true
2
+
3
+ module BrRailties
4
+ class FederalUnitsController < ApplicationController
5
+ active_scaffold :"br_railties/federal_unit" do |conf|
6
+ end
3
7
  end
4
8
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrRailties
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
+
11
+ active_scaffold :"br_railties/municipality" do |conf|
12
+ conf.columns[:federal_unit].form_ui = :select
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrRailties
4
+ def self.table_name_prefix
5
+ 'br_railties_'
6
+ end
7
+ end
@@ -1,8 +1,13 @@
1
- class BrRailties::FederalUnit < ActiveRecord::Base
2
- validates :acronym, presence: true, uniqueness: { :case_sensitive => false }, length: { in: 2..2 }
3
- validates :name, presence: true, uniqueness: { :case_sensitive => false }
1
+ # frozen_string_literal: true
4
2
 
5
- def acronym=(value)
6
- self[:acronym] = value.to_s.upcase
3
+ module BrRailties
4
+ class FederalUnit < ActiveRecord::Base
5
+ validates :acronym, presence: true, uniqueness: { case_sensitive: false }, length: { in: 2..2 }
6
+ validates :name, presence: true, uniqueness: { case_sensitive: false }
7
+ validates :ibge_code, presence: true, uniqueness: true, numericality: { greater_than: 0 }
8
+
9
+ def acronym=(value)
10
+ self[:acronym] = value.to_s.upcase
11
+ end
7
12
  end
8
13
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrRailties
4
+ class Municipality < ActiveRecord::Base
5
+ belongs_to :federal_unit, class_name: 'BrRailties::FederalUnit'
6
+
7
+ validates :federal_unit, presence: true
8
+ validates :name, presence: true, uniqueness: { case_sensitive: false, scope: [:federal_unit] }
9
+ validates :ibge_code, presence: true, uniqueness: true, numericality: { greater_than: 0 }
10
+ end
11
+ end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  BrRailties::Engine.routes.draw do
4
- resources(:federal_units) do
4
+ resources(:federal_units) { as_routes }
5
+ resources(:municipalities) do
5
6
  as_routes
7
+ record_select_routes
6
8
  end
7
9
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateBrRailtiesFederalUnits < ActiveRecord::Migration
4
+ def change
5
+ create_table :br_railties_federal_units do |t|
6
+ t.string :acronym
7
+ t.string :name
8
+
9
+ t.timestamps null: false
10
+ end
11
+ end
12
+ 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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddIbgeCodeToBrRailtiesFederalUnits < ActiveRecord::Migration
4
+ def change
5
+ add_column :br_railties_federal_units, :ibge_code, :integer
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddIbgeCodeToBrRailtiesMunicipalities < ActiveRecord::Migration
4
+ def change
5
+ add_column :br_railties_municipalities, :ibge_code, :integer
6
+ end
7
+ end
@@ -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
@@ -5,10 +5,8 @@ module BrRailties
5
5
  isolate_namespace BrRailties
6
6
 
7
7
  initializer :append_migrations do |app|
8
- unless app.root.to_s.match root.to_s
9
- config.paths['db/migrate'].expanded.each do |expanded_path|
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BrRailties
4
- VERSION = '0.1.0'
4
+ VERSION = '0.4.2'
5
5
  end
@@ -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.1.0
4
+ version: 0.4.2
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: 2019-02-26 00:00:00.000000000 Z
11
+ date: 2020-09-13 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: '0'
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: '0'
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: []
@@ -49,11 +63,21 @@ files:
49
63
  - app/assets/javascripts/br_railties.js
50
64
  - app/assets/stylesheets/br_railties.scss
51
65
  - app/controllers/br_railties/federal_units_controller.rb
66
+ - app/controllers/br_railties/municipalities_controller.rb
67
+ - app/models/br_railties.rb
52
68
  - app/models/br_railties/federal_unit.rb
69
+ - app/models/br_railties/municipality.rb
53
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
54
75
  - lib/br_railties.rb
55
76
  - lib/br_railties/engine.rb
77
+ - lib/br_railties/ibge/import/federal_units.rb
78
+ - lib/br_railties/ibge/import/municipalities.rb
56
79
  - lib/br_railties/version.rb
80
+ - lib/tasks/br_railties.rake
57
81
  homepage: https://github.com/esquilo-azul/br_railties
58
82
  licenses:
59
83
  - MIT
@@ -74,8 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
98
  - !ruby/object:Gem::Version
75
99
  version: '0'
76
100
  requirements: []
77
- rubyforge_project:
78
- rubygems_version: 2.7.7
101
+ rubygems_version: 3.0.8
79
102
  signing_key:
80
103
  specification_version: 4
81
104
  summary: Brazilian resources for Rails.