-
Notifications
You must be signed in to change notification settings - Fork 983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #6150 - users need taxonomy added on "all Users" #4111
fixes #6150 - users need taxonomy added on "all Users" #4111
Conversation
@kgaikwad, thanks for your PR! By analyzing the history of the files in this pull request, we identified @isratrade, @tbrisker and @amirfefer to be potential reviewers. |
say "This Migration will take time for updating organization & location records with all users for which ignore_types includes 'User' resource." | ||
Rake::Task['taxonomy:update_taxonomy'].invoke | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected.
78e96b4
to
5cd492a
Compare
5cd492a
to
04cf58e
Compare
app/models/taxonomy.rb
Outdated
@@ -51,6 +51,8 @@ def self.inherited(child) | |||
end | |||
} | |||
|
|||
scope :enabled_select_all_taxonomies, -> (taxable_type){where("ignore_types LIKE ?", "%#{[taxable_type].to_yaml}%") } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use spaces between -> and opening brace in lambda literals
4216e9f
to
f7e8950
Compare
I have updated the pull-request. |
f7e8950
to
7d40ec7
Compare
0fe1e69
to
902163e
Compare
I have updated the pull-request with modifications to solve complexity warning for file taxonomix.rb |
902163e
to
f370916
Compare
Thanks @kgaikwad, could you please rebase the PR? |
@ares , |
f370916
to
56d2862
Compare
app/helpers/form_helper.rb
Outdated
klass = Location | ||
else | ||
klass = nil | ||
options.merge!(:size => "col-md-10") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use options[:size] = "col-md-10" instead of options.merge!(:size => "col-md-10").
56d2862
to
dee2dc7
Compare
@ares , |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Kavita, patches improving this area are highly appreciated. In general I think this improves the current situation but does not solve it completely.
I think that it could get out of sync again in case a new organization or location ignoring users is added after some user already exists. Perhaps some callback on Taxonomy model should be added that will make sure all resources are linked if they're ignored.
Last but not least, this fixes the issue for User model only. Could it be generalized in taxonomix.rb so it applies to all taxable resources? If it's implemented this way and we can rely on having proper relations in DB between resource and taxonomy, I think it could simplify the existing code
I think more tests would be helpful since this is area is prone to errors. Covering all new methods and a test verifying that after taxonomy starts to ignore a resource class, every such $resource.taxonomies
includes this taxonomy
app/models/user.rb
Outdated
@@ -560,6 +561,19 @@ def verify_current_password | |||
end | |||
end | |||
|
|||
# assign all taxonomies for which ignore_type includes 'User' resource | |||
def add_select_all_taxonomies | |||
if self.new_record? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the condition could be moved to after_initialize :add_select_all_taxonomies, :if => :new_record?
so it's more obvious it does not happen for existing users
{'data-descendants' => obj.children_of_selected_organization_ids.to_json, | ||
'data-useds' => obj.used_organization_ids.to_json }.merge!(html_options[:organization]) %> | ||
'data-useds' => obj.used_organization_ids.to_json, | ||
'data-usedall' => enabled_select_all_organization_ids.to_json }.merge!(html_options[:organization]) %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation is a bit off,
also, usedall
should be split to data-used-all
desc <<-END_DESC | ||
This task will update organization & location records with all users for which ignore_types includes 'User' resource. | ||
END_DESC | ||
task :update_taxonomy => :environment do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the rake task should probably run in User.anonymous_admin do ... end
(required since recent changes, probably was not required back then)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried the scenario with newly created DB, this migration fails because user ANONYMOUS_ADMIN
not being present at that time. Added condition to handle this in rake task but is there any better way to run the rake task in case this user is not present?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The admin user should exist when this task is needed, since admins are seeded before taxonomies. If the database is not seeded at all, this task shouldn't be needed and can be skipped. The condition could check if Organization.any?
or if Location.any?
but it's similar to what you have already. So I guess it can remain as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay then I am keeping the condition as it is.
Thank you @ares
app/models/user.rb
Outdated
@@ -100,6 +100,7 @@ def self.name_format | |||
|
|||
after_create :welcome_mail | |||
after_create :set_default_widgets | |||
after_initialize :add_select_all_taxonomies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work for new users but I think we should do similar check for existing ones, e.g. when I update some user through API and select subset of organizations. I can't do this through WebUI because of disabled fields but API/hammer could still do it.
dee2dc7
to
5a51f36
Compare
@ares , Thank you for explaining in brief. As per your suggestions, I have modified the code and added before_save hook for all ignore_types as well as on taxonomy.
Pending Work - I will add test cases once code changes done. |
@kgaikwad, this pull request is currently not mergeable. Please rebase against the develop branch and push again. If you have a remote called 'upstream' that points to this repository, you can do this by running:
This message was auto-generated by Foreman's prprocessor |
@kgaikwad hello, is this something we could revisit after 1.24 branching? Do you think you'll have capacity to finish it? Improvements in this area are more than welcome. |
Thank you @ares. |
6c32e8b
to
137a7b6
Compare
137a7b6
to
d74da02
Compare
d74da02
to
fe70805
Compare
1e004f2
to
707b743
Compare
707b743
to
bf5f00e
Compare
@ares, EDIT - I have tested with few general scenarios:
|
With this commit, if "All users" is ticked then locations and organizations are added to users. On edit user page, you can not edit this taxonomy as they are disabled with tooltip "Select all option enabled for this taxonomy".
bf5f00e
to
3c45322
Compare
test/models/taxonomy_test.rb
Outdated
@@ -73,4 +73,24 @@ class TaxonomyTest < ActiveSupport::TestCase | |||
location.save | |||
assert_match /expecting organizations/, location.errors.messages[:organizations].first | |||
end | |||
|
|||
test "#taxonomy_ids_by_ignore_type should return ids of taxonomy_type for which 'Select All' option is checked for a resource." do | |||
FactoryBot.create(:organization, :ignore_types => [ 'User' ]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
app/models/taxonomy.rb
Outdated
klass_ids_meth = klass.underscore + '_ids' | ||
existing_ids = self.send(klass_ids_meth) | ||
next if (klass_obj_ids - existing_ids).blank? | ||
self.send("#{klass_ids_meth}=", klass_obj_ids) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
app/models/taxonomy.rb
Outdated
ignore_types.each do |klass| | ||
klass_obj_ids = klass.constantize.unscoped.pluck(:id) | ||
klass_ids_meth = klass.underscore + '_ids' | ||
existing_ids = self.send(klass_ids_meth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
app/models/taxonomy.rb
Outdated
Template.where(:default => true).select(&:valid?).each do |template| | ||
template_class_string = template.class.to_s | ||
unless self.ignore_types.include?(template_class_string) | ||
self.send(template_class_string.underscore.pluralize.to_s) << template |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
app/models/taxonomy.rb
Outdated
send("#{association}=", send(association) + templates.select(&:valid?)) | ||
Template.where(:default => true).select(&:valid?).each do |template| | ||
template_class_string = template.class.to_s | ||
unless self.ignore_types.include?(template_class_string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
app/models/concerns/taxonomix.rb
Outdated
return if (taxonomy_ids_by_ignore_type - existing_obj_ids).blank? | ||
|
||
taxonomy_ids_by_ignore_type.concat(existing_obj_ids).uniq! | ||
self.send("#{taxonomy_ids_meth}=", taxonomy_ids_by_ignore_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
app/models/concerns/taxonomix.rb
Outdated
|
||
def assign_taxonomy_ids(taxonomy_class_name, taxonomy_ids_by_ignore_type) | ||
taxonomy_ids_meth = taxonomy_class_name.underscore + '_ids' | ||
existing_obj_ids = self.send(taxonomy_ids_meth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style/RedundantSelf: Redundant self detected.
closing for now, thanks @kgaikwad ! |
With this commit, if "All users" is ticked then
locations and organizations are added to users. On edit user page,
you can not edit this taxonomy as they are disabled with tooltip
"Select all option enabled for this taxonomy".
Likewise, it will fix 'select all' option functionality for other ignore_types as well.