egov_utils 0.2.13 → 0.3.0

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: '0180df0129a941cc5e555901b831867c2540fb9fba09d2ae8ac4d174e07606d7'
4
- data.tar.gz: 9790504b20a2987452da2433aa0364de5d0d6b1ad59d3646cfdf3d2b761dda4a
3
+ metadata.gz: db8d02864512bcde337488cdcff4446ab6603d612ff7fe85aebcae592abad8a5
4
+ data.tar.gz: 887cb22e5ead25efd16576a9c9481336a5e669a57f485d18933b6be05462f495
5
5
  SHA512:
6
- metadata.gz: c458ee913a5c0b9c6727db5283b971cec87fcbea447ee540d665201addfed1a3ca396fe5019d9d4128348bf717c711330c05e1654fc1bef4849561d90a2935b6
7
- data.tar.gz: f455d3005cdaa372d499ac0fc42a288814554249cf8b909ac625b11c10673d098647fefd94c2c7e2fa994af5eecca5fca4473058a7740f70d1f9fdca648632f4
6
+ metadata.gz: 621e9e7fbcf99247224f07289dad00275d4f06251e17e4f9cf7e482bed13692cca82690fe880b81d83df56684ff6be65a7c987c6e934621b67821d65dab2f42a
7
+ data.tar.gz: b8b6df91a246ad9216f149bf5eea8fbad81947830ae553763a49aa8906ddffbd7807b12163f5dbe0c6086307a9bf4aeadff72cd7397a15a071631538b7980203
@@ -30,6 +30,7 @@ window.eGovUtilities =
30
30
  eGovUtilities.initDatepickers($container)
31
31
  eGovUtilities.initSelect2($container)
32
32
  eGovUtilities.initFileFields($container)
33
+ eGovUtilities.initPeople($container)
33
34
 
34
35
  clearBeforeCache: ($container)->
35
36
  $container ||= $(document)
@@ -53,6 +54,15 @@ window.eGovUtilities =
53
54
  pickers = $('[type="datetime-local"][data-provide="datepicker"]', $container)
54
55
  pickers.parent().datetimepicker()
55
56
 
57
+ initPeople: ($container)->
58
+ $container ||= $(document)
59
+ $('.person-form .person-type-toggle', $container).on('change', (evt)->
60
+ val = $(this).val();
61
+ $form = $(this).closest('.person-form');
62
+ $form.find('.natural_person').toggle( val == 'natural').find(':input').prop('disabled', val != 'natural');
63
+ $form.find('.legal_person').toggle( val == 'legal').find(':input').prop('disabled', val != 'legal');
64
+ ).change()
65
+
56
66
  initSelect2: ($container)->
57
67
  $container ||= $(document)
58
68
  $('[data-provide="select2"]', $container).select2()
@@ -0,0 +1,13 @@
1
+ module EgovUtils
2
+ class LegalFormAttribute < AzaharaSchema::Attribute
3
+
4
+ def initialize(model, name)
5
+ super(model, name, 'love')
6
+ end
7
+
8
+ def available_values
9
+ @available_values ||= EgovUtils::LegalForm.all.collect{|lf| [lf.name, lf.key] }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module EgovUtils
2
+ class AbstractPerson < ApplicationRecord
3
+
4
+ self.abstract_class = true
5
+
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module EgovUtils
2
+ class LegalPerson < AbstractPerson
3
+
4
+ belongs_to :person, class_name: 'EgovUtils::Person'
5
+
6
+ def fullname
7
+ name
8
+ end
9
+
10
+ def to_s
11
+ "#{fullname} (#{ico})"
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module EgovUtils
2
+ class NaturalPerson < AbstractPerson
3
+
4
+ belongs_to :person, class_name: 'EgovUtils::Person'
5
+
6
+ validates :firstname, :lastname, :birth_date, presence: true
7
+ validates :birth_date, uniqueness: { scope: [:firstname, :lastname] }, birthday: true
8
+
9
+ def fullname
10
+ firstname.to_s + ' ' + lastname.to_s
11
+ end
12
+
13
+ def to_s
14
+ "#{fullname} (#{I18n.t(:text_born_on_at, place: birth_place, date: I18n.l(birth_date.to_date))})"
15
+ end
16
+
17
+ end
18
+ end
@@ -2,18 +2,28 @@ module EgovUtils
2
2
  class Person < ApplicationRecord
3
3
 
4
4
  belongs_to :residence, class_name: 'EgovUtils::Address', optional: true
5
+ has_one :natural_person
6
+ has_one :legal_person
5
7
 
6
- validates :firstname, :lastname, :birth_date, presence: true
7
- validates :birth_date, uniqueness: { scope: [:firstname, :lastname] }, birthday: true
8
+ enum person_type: { natural: 1, legal: 16 }
8
9
 
9
- accepts_nested_attributes_for :residence
10
+ validates :person_entity, presence: true
10
11
 
11
- def fullname
12
- firstname.to_s + ' ' + lastname.to_s
12
+ accepts_nested_attributes_for :residence, :natural_person, :legal_person
13
+
14
+ delegate :fullname, :fullname=, to: :person_entity
15
+
16
+ def person_entity
17
+ case person_type
18
+ when 'natural'
19
+ natural_person
20
+ when 'legal'
21
+ legal_person
22
+ end
13
23
  end
14
24
 
15
25
  def to_s
16
- "#{fullname} (#{I18n.t(:text_born_on_at, place: birth_place, date: I18n.l(birth_date.to_date))})"
26
+ person_entity.to_s
17
27
  end
18
28
 
19
29
  end
@@ -0,0 +1,9 @@
1
+ module EgovUtils
2
+ class LegalForm < Love
3
+
4
+ def self.find_by_key(key)
5
+ where(key: key).first
6
+ end
7
+
8
+ end
9
+ end
@@ -1,5 +1,21 @@
1
1
  require 'activeresource'
2
2
 
3
+
4
+ module EgovUtils
5
+ module AzaharaJsonResourceFormat
6
+ include ActiveResource::Formats::JsonFormat
7
+ extend self
8
+
9
+ def decode(json)
10
+ resources = super(json)
11
+ (resources.is_a?(Hash) && resources.key?('entities')) ? resources['entities'] : resources
12
+ end
13
+
14
+ end
15
+ end
16
+
17
+
18
+
3
19
  module EgovUtils
4
20
  class Love < ::ActiveResource::Base
5
21
 
@@ -9,6 +25,8 @@ module EgovUtils
9
25
  end
10
26
 
11
27
  self.site = "#{config['love_url'] || Rails.configuration.try(:love_url)}/api/v1/"
28
+ self.format = EgovUtils::AzaharaJsonResourceFormat
29
+
12
30
 
13
31
  def self.where(clauses = {})
14
32
  raise ArgumentError, "expected a clauses Hash, got #{clauses.inspect}" unless clauses.is_a? Hash
@@ -0,0 +1,5 @@
1
+ module EgovUtils
2
+ class AbstractPersonSchema < AzaharaSchema::ModelSchema
3
+
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module EgovUtils
2
+ class LegalPersonSchema < AzaharaSchema::ModelSchema
3
+
4
+ def self.attribute(model, attr_name, attr_type=nil)
5
+ case attr_name
6
+ when 'legal_form'
7
+ LegalFormAttribute.new(model, attr_name)
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ module EgovUtils
2
+ class NaturalPersonSchema < AzaharaSchema::ModelSchema
3
+
4
+ def main_attribute_name
5
+ 'fullname'
6
+ end
7
+
8
+ def initialize_available_attributes
9
+ @available_attributes ||= []
10
+ @available_attributes << AzaharaSchema::DerivedAttribute.new(model, 'fullname', :concat, 'firstname', 'lastname', schema: self)
11
+ super
12
+ end
13
+
14
+ end
15
+ end
@@ -7,7 +7,7 @@ module EgovUtils
7
7
 
8
8
  def initialize_available_attributes
9
9
  @available_attributes ||= []
10
- @available_attributes << AzaharaSchema::DerivedAttribute.new(model, 'fullname', :concat, 'firstname', 'lastname', schema: self)
10
+ @available_attributes << AzaharaSchema::DerivedAttribute.new(model, 'fullname', :concat, 'natural_person-fullname', 'legal_person-name', schema: self)
11
11
  super
12
12
  end
13
13
 
@@ -1,8 +1,18 @@
1
- .row
2
- .col-12.col-sm-6= form.text_field(:firstname)
3
- .col-12.col-sm-6= form.text_field(:lastname)
4
- .col-12.col-sm-6= form.date_field(:birth_date, data: {'date-view-mode' => 'decades', 'date-min-date' => Date.new(1920, 1, 1).to_s, 'date-max-date' => Date.today.to_s, 'date-use-current' => false, 'date-view-date' => Date.new(1950, 1, 1).to_s })
5
- .col-12.col-sm-6= form.text_field(:birth_place)
6
- .col-12
7
- = form.fields_for(:residence, person.residence || EgovUtils::Address.new) do |fields|
8
- = render 'egov_utils/addresses/form', form: fields, address: fields.object
1
+ .person-form
2
+ .row
3
+ .col-12= form.select :person_type, enum_values_for_select(person.class, :person_type), {}, class: 'person-type-toggle'
4
+ .natural_person.row
5
+ = form.fields_for(:natural_person, person.natural_person || EgovUtils::NaturalPerson.new) do |fields|
6
+ .col-12.col-sm-6= fields.text_field(:firstname)
7
+ .col-12.col-sm-6= fields.text_field(:lastname)
8
+ .col-12.col-sm-6= fields.date_field(:birth_date, data: {'date-view-mode' => 'decades', 'date-min-date' => Date.new(1920, 1, 1).to_s, 'date-max-date' => Date.today.to_s, 'date-use-current' => false, 'date-view-date' => Date.new(1950, 1, 1).to_s })
9
+ .col-12.col-sm-6= fields.text_field(:birth_place)
10
+ .legal_person.row
11
+ = form.fields_for(:legal_person, person.legal_person || EgovUtils::LegalPerson.new) do |fields|
12
+ .col-12= fields.text_field(:name)
13
+ .col-12.col-sm-6= fields.text_field(:ico)
14
+ .col-12.col-sm-6= fields.select2 :legal_form, list_values_for_select(azahara_attribute_for(EgovUtils::LegalPerson, 'legal_form'))
15
+ .row
16
+ .col-12
17
+ = form.fields_for(:residence, person.residence || EgovUtils::Address.new) do |fields|
18
+ = render 'egov_utils/addresses/form', form: fields, address: fields.object
@@ -1 +1,2 @@
1
- = current_user.fullname
1
+ %h3= current_user.fullname
2
+ = current_user.mail || current_user.login
@@ -8,7 +8,13 @@
8
8
  </head>
9
9
 
10
10
  <body>
11
- Dobrý den,<br />
11
+ <p>
12
+ <% if content_for?(:greeting) %>
13
+ <%= yield :greeting %>
14
+ <% else %>
15
+ Dobrý den,
16
+ <% end %>
17
+ </p>
12
18
  <br />
13
19
  <%= yield %>
14
20
  <br />
@@ -1,4 +1,8 @@
1
+ <% if content_for?(:greeting) %>
2
+ <%= yield :greeting %>
3
+ <% else %>
1
4
  Dobrý den,
5
+ <% end %>
2
6
 
3
7
  <%= yield %>
4
8
 
@@ -46,6 +46,7 @@ cs:
46
46
  label_new: Nový
47
47
  label_edit: Upravit
48
48
  label_profile: Profil
49
+ label_change_password: Změnit heslo
49
50
  label_login: Přihlásit
50
51
  label_logout: Odhlásit
51
52
  label_signup: Registrovat
@@ -104,13 +105,22 @@ cs:
104
105
  region: Kraj
105
106
  country: Země
106
107
  egov_utils/person:
108
+ person_type: Typ osoby
109
+ person_types:
110
+ natural: Fyzická
111
+ legal: Právnická
112
+ egov_utils/abstract_person:
107
113
  fullname: Celé jméno
114
+ egov_utils/natural_person:
108
115
  firstname: Jméno
109
116
  lastname: Příjmení
110
117
  birth_date: Datum narození
111
118
  birth_place: Místo narození
112
119
  residence: Bydliště
113
-
120
+ egov_utils/legal_person:
121
+ name: Název
122
+ ico: IČO
123
+ legal_form: Právní forma
114
124
 
115
125
  models_errors: &my_model_errors
116
126
  profile:
@@ -0,0 +1,9 @@
1
+ class RenamePeopleToNaturalPeople < ActiveRecord::Migration[5.1]
2
+ def up
3
+ rename_table :egov_utils_people, :egov_utils_natural_people
4
+ end
5
+
6
+ def down
7
+ rename_table :egov_utils_natural_people, :egov_utils_people
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class CreateEgovUtilsPeopleBase < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :egov_utils_people do |t|
4
+ t.integer :person_type
5
+ t.string :joid
6
+ t.references :residence, foreign_key: {to_table: :egov_utils_addresses}
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ class JoinPeopleTables < ActiveRecord::Migration[5.1]
2
+ def up
3
+ add_reference :egov_utils_natural_people, :person, foreign_key: { to_table: :egov_utils_people }
4
+ select_rows("SELECT id, residence_id FROM egov_utils_natural_people").each do |row|
5
+ if row.second
6
+ value = exec_insert("INSERT INTO egov_utils_people (person_type, residence_id) VALUES (1, #{row.second})", "person create")
7
+ else
8
+ value = exec_insert("INSERT INTO egov_utils_people (person_type) VALUES (1)", "person create")
9
+ end
10
+ id_value = ActiveRecord::Base.connection.send(:last_inserted_id, value)
11
+ execute("UPDATE egov_utils_natural_people SET person_id = #{id_value} WHERE id = #{row.first}")
12
+ end
13
+ # remove_reference :egov_utils_natural_people, :residence
14
+ end
15
+
16
+ def down
17
+ add_reference :egov_utils_natural_people, :residence, foreign_key: { to_table: :egov_utils_addresses }
18
+ EgovUtils::NaturalPerson.preload(:person).all.each do |np|
19
+ np.update_column(:residence_id, np.person.residence_id)
20
+ end
21
+ remove_reference :egov_utils_natural_people, :person, foreign_key: { to_table: :egov_utils_people }
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ class CreateEgovUtilsLegalPeople < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :egov_utils_legal_people do |t|
4
+ t.references :person, foreign_key: { to_table: :egov_utils_people }
5
+ t.string :name
6
+ t.string :ico
7
+ t.integer :legal_form
8
+ end
9
+ end
10
+ end
data/lib/egov_utils.rb CHANGED
@@ -5,6 +5,7 @@ require 'bootstrap_form'
5
5
  require 'momentjs-rails'
6
6
 
7
7
  require 'egov_utils/fileuid'
8
+ require 'egov_utils/model_permissions'
8
9
 
9
10
  require "egov_utils/engine"
10
11
 
@@ -0,0 +1,52 @@
1
+ module EgovUtils
2
+ class ModelPermissions
3
+
4
+ def self.build(model, user)
5
+ klass = model
6
+ klasses = [klass]
7
+ while klass != klass.base_class
8
+ klass = klass.superclass
9
+ klasses << klass
10
+ end
11
+ klasses.each do |kls|
12
+ perm_class = "#{kls.name}Permissions".safe_constantize
13
+ return perm_class.new(user) if perm_class
14
+ end
15
+ EgovUtils::ModelPermissions.new(model, user)
16
+ end
17
+
18
+ attr_reader :model, :user
19
+
20
+ def initialize(model, user)
21
+ @model, @user = model, user
22
+ end
23
+
24
+ def readable_attributes(action=:show)
25
+ model.column_names
26
+ end
27
+
28
+ def basic_editable_attributes(action=:update)
29
+ model.column_names - ['id', 'updated_at', 'created_at']
30
+ end
31
+
32
+ def basic_editable_attributes_for(entity, action=:update)
33
+ basic_editable_attributes(action)
34
+ end
35
+
36
+ def editable_attributes(action=:update)
37
+ attributes = basic_editable_attributes(action)
38
+ assocs = model.reflect_on_all_associations.select{|assoc| model.method_defined?("#{assoc.name}_attributes=".to_sym) }
39
+ attributes << assocs.each_with_object({}) {|assoc, obj| obj["#{assoc.name}_attributes"] = self.class.build(assoc.klass, user).editable_attributes(action) }
40
+ attributes
41
+ end
42
+
43
+ #TODO nested attributes should take entity as well - what to do with has_many?
44
+ def editable_attributes_for(entity, action=:update)
45
+ attributes = basic_editable_attributes(action)
46
+ assocs = model.reflect_on_all_associations.select{|assoc| model.method_defined?("#{assoc.name}_attributes=".to_sym) }
47
+ attributes << assocs.each_with_object({}) {|assoc, obj| obj["#{assoc.name}_attributes"] = self.class.build(assoc.klass, user).editable_attributes_for(action) }
48
+ attributes
49
+ end
50
+
51
+ end
52
+ end
@@ -59,6 +59,14 @@ module EgovUtils
59
59
  return false
60
60
  end
61
61
 
62
+ def editable_attributes(model, action=:update)
63
+ EgovUtils::ModelPermissions.build(model, current_user).editable_attributes(action)
64
+ end
65
+
66
+ def editable_attributes_for(entity, action=:update)
67
+ EgovUtils::ModelPermissions.build(entity.type, current_user).editable_attributes_for(entity, action)
68
+ end
69
+
62
70
  protected
63
71
  def find_current_user
64
72
  # existing session
@@ -1,3 +1,3 @@
1
1
  module EgovUtils
2
- VERSION = '0.2.13'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: egov_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ondřej Ezr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-21 00:00:00.000000000 Z
11
+ date: 2018-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -310,14 +310,14 @@ dependencies:
310
310
  requirements:
311
311
  - - "~>"
312
312
  - !ruby/object:Gem::Version
313
- version: '0.2'
313
+ version: '0.3'
314
314
  type: :runtime
315
315
  prerelease: false
316
316
  version_requirements: !ruby/object:Gem::Requirement
317
317
  requirements:
318
318
  - - "~>"
319
319
  - !ruby/object:Gem::Version
320
- version: '0.2'
320
+ version: '0.3'
321
321
  - !ruby/object:Gem::Dependency
322
322
  name: egon_gate
323
323
  requirement: !ruby/object:Gem::Requirement
@@ -388,6 +388,7 @@ files:
388
388
  - app/attributes/egov_utils/all_role_names.rb
389
389
  - app/attributes/egov_utils/district.rb
390
390
  - app/attributes/egov_utils/full_address.rb
391
+ - app/attributes/egov_utils/legal_form_attribute.rb
391
392
  - app/attributes/egov_utils/region.rb
392
393
  - app/controllers/egov_utils/addresses_controller.rb
393
394
  - app/controllers/egov_utils/application_controller.rb
@@ -409,17 +410,24 @@ files:
409
410
  - app/mailers/egov_utils/application_mailer.rb
410
411
  - app/mailers/egov_utils/user_mailer.rb
411
412
  - app/models/ability.rb
413
+ - app/models/egov_utils/abstract_person.rb
412
414
  - app/models/egov_utils/address.rb
413
415
  - app/models/egov_utils/application_record.rb
414
416
  - app/models/egov_utils/audit_detail.rb_bac
415
417
  - app/models/egov_utils/audit_record.rb_bac
416
418
  - app/models/egov_utils/group.rb
419
+ - app/models/egov_utils/legal_person.rb
420
+ - app/models/egov_utils/natural_person.rb
417
421
  - app/models/egov_utils/person.rb
418
422
  - app/models/egov_utils/principal.rb
419
423
  - app/models/egov_utils/user.rb
424
+ - app/resources/egov_utils/legal_form.rb
420
425
  - app/resources/egov_utils/love.rb
421
426
  - app/resources/egov_utils/organization.rb
427
+ - app/schemas/egov_utils/abstract_person_schema.rb
422
428
  - app/schemas/egov_utils/address_schema.rb
429
+ - app/schemas/egov_utils/legal_person_schema.rb
430
+ - app/schemas/egov_utils/natural_person_schema.rb
423
431
  - app/schemas/egov_utils/person_schema.rb
424
432
  - app/schemas/egov_utils/user_schema.rb
425
433
  - app/validators/birthday_validator.rb
@@ -481,6 +489,10 @@ files:
481
489
  - db/migrate/20171115142450_add_confirmation_code_to_users.rb
482
490
  - db/migrate/20180125133500_add_password_generation_to_egov_utils_users.rb
483
491
  - db/migrate/20180126131416_create_egov_utils_groups_users.rb
492
+ - db/migrate/20180403132853_rename_people_to_natural_people.rb
493
+ - db/migrate/20180403133155_create_egov_utils_people_base.rb
494
+ - db/migrate/20180403141531_join_people_tables.rb
495
+ - db/migrate/20180403150556_create_egov_utils_legal_people.rb
484
496
  - lib/bootstrap_form/check_box_patch.rb
485
497
  - lib/bootstrap_form/custom_file_field.rb
486
498
  - lib/bootstrap_form/datetimepicker.rb
@@ -495,6 +507,7 @@ files:
495
507
  - lib/egov_utils/helpers/form_helper.rb
496
508
  - lib/egov_utils/helpers/tags/datetime_field_patch.rb
497
509
  - lib/egov_utils/helpers/tags/fileuid_field.rb
510
+ - lib/egov_utils/model_permissions.rb
498
511
  - lib/egov_utils/redmine/issue.rb
499
512
  - lib/egov_utils/routes/admin_constraint.rb
500
513
  - lib/egov_utils/settings.rb