egov_utils 0.3.1 → 0.3.2

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: eae0f0f3e3b9e24657d648a0ae710b0ea209b41385303a0dfe98996475f319b2
4
- data.tar.gz: 5cf138da356f3ac4068f58a0e18d9fb7a1e51474fa7669c7c07a8e788c7a951b
3
+ metadata.gz: f91e4fd34931a6237076b8c8e859cb184c5d61f1ae1c2310800d795d03eee04a
4
+ data.tar.gz: da90da14b07d985a456c5742fc17ef50cafb40718ad91e6bc69b23bc55a65f26
5
5
  SHA512:
6
- metadata.gz: f657dbbc3c0b06c7a832874d524aaf3bc9c42584c97d1d721c3fe229dd2ecf05a4d0f79ae9157dc35cf5c129d5358c79b9ccaea67d49405cfc6ded7c910deb5d
7
- data.tar.gz: d7f627e6fa125e04d8520bb6a4fd21c76c1c35415f2756fa9b07a370dfa7fba6ea98ad92041e6f0bd43f64680d13d0d2dcc5be4b5674ea7023c6192d1a9cded0
6
+ metadata.gz: defc11887b3528370c76e42de8a7c6b93cb48ca7067675f68378a9e8ee4ef0832d83328e606d925a70b222fa2b2e4801ad1e81757015b32082e8cfb40f6c1df2
7
+ data.tar.gz: 923f682a552728d002baef7a27101d976c209318fd920104994db7acc624ff4595d1666971af5dc3132d7d4f05cc57f2bfa2a3fa7ad01079b0d87c55132f5d89
@@ -1,4 +1,12 @@
1
1
 
2
+ window.set_disabled_observer = (element, trigger) ->
3
+ observer = new MutationObserver (mutations) ->
4
+ mutations.forEach (mutation)->
5
+ if mutation.attributeName == "disabled"
6
+ trigger();
7
+ observer.observe(element, {attributes: true})
8
+ observer
9
+
2
10
  window.eGovUtilities =
3
11
  setup: ()->
4
12
  # select2 defaults
@@ -56,16 +64,20 @@ window.eGovUtilities =
56
64
 
57
65
  initPeople: ($container)->
58
66
  $container ||= $(document)
59
- $('.person-form .person-type-toggle', $container).on('change', (evt)->
67
+ $type_toggles = $('.person-form .person-type-toggle', $container)
68
+ $type_toggles.on('change', (evt)->
60
69
  val = $(this).val();
61
70
  $form = $(this).closest('.person-form');
62
71
  $form.find('.natural_person').toggle( val == 'natural').find(':input').prop('disabled', val != 'natural');
63
72
  $form.find('.legal_person').toggle( val == 'legal').find(':input').prop('disabled', val != 'legal');
64
- ).change()
73
+ )
74
+ $type_toggles.each (index)->
75
+ $el = $(this)
76
+ $el.change() if $el.is(':visible')
65
77
 
66
78
  initSelect2: ($container)->
67
79
  $container ||= $(document)
68
- $('[data-provide="select2"]', $container).select2()
80
+ $('[data-provide="select2"]', $container).select2(width: null)
69
81
 
70
82
  destroySelect2: ($container)->
71
83
  $container ||= $(document)
@@ -85,6 +97,27 @@ window.eGovUtilities =
85
97
  # )
86
98
  $cpyBtn
87
99
 
100
+ toggleContainerForm: ($container, toggle_state) ->
101
+ $container.toggle(toggle_state)
102
+ $container.find(':input').prop('disabled', !toggle_state)
103
+
104
+ toggableForm: ($toggler, attr_names, get_state) ->
105
+ get_state ||= ($toggler)->
106
+ input_type = $toggler.attr('type')
107
+ state = !$toggler.prop('disabled')
108
+ if input_type == 'checkbox'
109
+ state = state && $toggler.is(':checked')
110
+ else
111
+ state = state && !$toggler.val() && $toggler.val() != ''
112
+ return state
113
+
114
+ $attributes = $toggler.closest('.'+attr_names).find('.'+attr_names+'-attributes')
115
+
116
+ eGovUtilities.toggleContainerForm($attributes, get_state($toggler))
117
+ $toggler.on 'change', (evt) ->
118
+ eGovUtilities.toggleContainerForm($attributes, get_state($toggler))
119
+
120
+
88
121
  initModal: (modalId, options)->
89
122
  options = options || {}
90
123
  modalId = modalId || 'modal'
@@ -16,16 +16,20 @@ module EgovUtils
16
16
  end
17
17
 
18
18
  def members
19
-
19
+ if ldap?
20
+ EgovUtils::User.where(login: ldap_members.collect{|m| m[:login] })
21
+ else
22
+ users
23
+ end
20
24
  end
21
25
 
22
26
  def ldap_members
23
- Rails.cache.fetch("#{cache_key}/ldap_members", expires_in: 2.hours) do
24
- if provider.present?
27
+ if provider.present?
28
+ Rails.cache.fetch("#{cache_key}/ldap_members", expires_in: 2.hours) do
25
29
  auth_source.group_members(ldap_uid)
26
- else
27
- []
28
30
  end
31
+ else
32
+ []
29
33
  end
30
34
  end
31
35
 
@@ -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, 'lastname', 'firstname', schema: self)
11
11
  super
12
12
  @available_attributes << AllRoleNames.new(model, 'all_role_names', 'string')
13
13
  end
@@ -41,7 +41,9 @@
41
41
  }
42
42
  })
43
43
  window.setTimeout(function(){
44
- $('.country-select', $fields).change();
44
+ var $country_select = $('.country-select', $fields);
45
+ if( $country_select.is(':visible') )
46
+ $country_select.change();
45
47
  }, 1500);
46
48
 
47
49
  $('.address-validator-btn', $fields).on('click', function(evt) {
@@ -5,7 +5,7 @@
5
5
  = form.fields_for(:natural_person, person.natural_person || EgovUtils::NaturalPerson.new) do |fields|
6
6
  .col-12.col-sm-6= fields.text_field(:firstname)
7
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 })
8
+ .col-12.col-sm-6= fields.date_field(:birth_date, min: Date.new(1920, 1, 1), max: Date.today, data: {'date-view-mode' => 'decades', 'date-use-current' => false, 'date-view-date' => Date.new(1950, 1, 1).to_s })
9
9
  .col-12.col-sm-6= fields.text_field(:birth_place)
10
10
  .legal_person.row
11
11
  = form.fields_for(:legal_person, person.legal_person || EgovUtils::LegalPerson.new) do |fields|
@@ -1,7 +1,7 @@
1
1
  module EgovUtils
2
2
  class ModelPermissions
3
3
 
4
- def self.build(model, user)
4
+ def self.build(model, ability)
5
5
  klass = model
6
6
  klasses = [klass]
7
7
  while klass != klass.base_class
@@ -10,15 +10,15 @@ module EgovUtils
10
10
  end
11
11
  klasses.each do |kls|
12
12
  perm_class = "#{kls.name}Permissions".safe_constantize
13
- return perm_class.new(user) if perm_class
13
+ return perm_class.new(ability) if perm_class
14
14
  end
15
- EgovUtils::ModelPermissions.new(model, user)
15
+ EgovUtils::ModelPermissions.new(model, ability)
16
16
  end
17
17
 
18
- attr_reader :model, :user
18
+ attr_reader :model, :ability
19
19
 
20
- def initialize(model, user)
21
- @model, @user = model, user
20
+ def initialize(model, ability)
21
+ @model, @ability = model, ability
22
22
  end
23
23
 
24
24
  def readable_attributes(action=:show)
@@ -33,20 +33,55 @@ module EgovUtils
33
33
  basic_editable_attributes(action)
34
34
  end
35
35
 
36
+ def assignable_associations
37
+ model.reflect_on_all_associations.select{|assoc| model.method_defined?("#{assoc.name}_attributes=".to_sym) }
38
+ end
39
+
36
40
  def editable_attributes(action=:update)
41
+ return [] unless ability.can?(action, model)
37
42
  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) }
43
+ assignable_associations.each{|assoc| add_editable_association_attributes(attributes, assoc, action) }
40
44
  attributes
41
45
  end
42
46
 
43
47
  #TODO nested attributes should take entity as well - what to do with has_many?
44
48
  def editable_attributes_for(entity, action=:update)
49
+ return [] unless ability.can?(action, entity)
45
50
  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) }
51
+ assignable_associations.each{|assoc| add_editable_association_attributes_for(attributes, assoc, entity, action) }
48
52
  attributes
49
53
  end
50
54
 
55
+ def add_editable_association_attributes(attributes, assoc, action)
56
+ assoc_permissions = self.class.build(assoc.klass, ability)
57
+
58
+ assoc_attributes = assoc_permissions.editable_attributes(action)
59
+ assoc_attributes.unshift('id') if ability.can?(:update, assoc.klass)
60
+ assoc_attributes_hash = { "#{assoc.name}_attributes" => assoc_attributes }
61
+
62
+ case assoc.macro
63
+ when :has_many
64
+ attributes << assoc_attributes_hash
65
+ when :has_one, :belongs_to
66
+ if ability.can?(:update, assoc.klass)
67
+ attributes << assoc.foreign_key if assoc.macro == :belongs_to
68
+ attributes << assoc_attributes_hash
69
+ else
70
+ attributes << assoc_attributes_hash
71
+ end
72
+ else
73
+ attributes << assoc_attributes_hash
74
+ end
75
+ end
76
+
77
+ def add_editable_association_attributes_for(attributes, assoc, entity, action)
78
+ case assoc.macro
79
+ when :has_many
80
+
81
+ else
82
+ add_editable_association_attributes(attributes, assoc, action)
83
+ end
84
+ end
85
+
51
86
  end
52
87
  end
@@ -60,11 +60,11 @@ module EgovUtils
60
60
  end
61
61
 
62
62
  def editable_attributes(model, action=:update)
63
- EgovUtils::ModelPermissions.build(model, current_user).editable_attributes(action)
63
+ EgovUtils::ModelPermissions.build(model, current_ability).editable_attributes(action)
64
64
  end
65
65
 
66
66
  def editable_attributes_for(entity, action=:update)
67
- EgovUtils::ModelPermissions.build(entity.type, current_user).editable_attributes_for(entity, action)
67
+ EgovUtils::ModelPermissions.build(entity.type, current_ability).editable_attributes_for(entity, action)
68
68
  end
69
69
 
70
70
  protected
@@ -1,3 +1,3 @@
1
1
  module EgovUtils
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
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.3.1
4
+ version: 0.3.2
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-04-06 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails