Skip to content
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

Conversation

kgaikwad
Copy link
Member

@kgaikwad kgaikwad commented Dec 14, 2016

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.

@mention-bot
Copy link

@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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch 2 times, most recently from 78e96b4 to 5cd492a Compare December 15, 2016 06:47
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 5cd492a to 04cf58e Compare January 25, 2017 11:02
@@ -51,6 +51,8 @@ def self.inherited(child)
end
}

scope :enabled_select_all_taxonomies, -> (taxable_type){where("ignore_types LIKE ?", "%#{[taxable_type].to_yaml}%") }

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

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch 2 times, most recently from 4216e9f to f7e8950 Compare January 25, 2017 13:25
@kgaikwad
Copy link
Member Author

I have updated the pull-request.
codeclimate is showing complexity warnings for taxonomix.rb file.
But I am not getting how to resolve it.
Please provide me some pointers so that I will do further modifications.

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from f7e8950 to 7d40ec7 Compare January 25, 2017 14:20
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch 2 times, most recently from 0fe1e69 to 902163e Compare February 10, 2017 06:26
@kgaikwad
Copy link
Member Author

I have updated the pull-request with modifications to solve complexity warning for file taxonomix.rb

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 902163e to f370916 Compare March 9, 2017 09:01
@ares
Copy link
Member

ares commented Jul 19, 2017

Thanks @kgaikwad, could you please rebase the PR?

@kgaikwad
Copy link
Member Author

@ares ,
Yes, I will update PR soon.

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from f370916 to 56d2862 Compare July 19, 2017 12:48
klass = Location
else
klass = nil
options.merge!(:size => "col-md-10")

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").

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 56d2862 to dee2dc7 Compare July 19, 2017 12:52
@kgaikwad
Copy link
Member Author

@ares ,
Done. I have updated the PR.
Please let me know if any further changes required.

Copy link
Member

@ares ares left a 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

@@ -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?
Copy link
Member

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]) %>
Copy link
Member

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
Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member

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.

Copy link
Member Author

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

@@ -100,6 +100,7 @@ def self.name_format

after_create :welcome_mail
after_create :set_default_widgets
after_initialize :add_select_all_taxonomies
Copy link
Member

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.

@kgaikwad
Copy link
Member Author

@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.
I have queries regarding few points:

  • As you mentioned that code modifications will simplify the existing code.
    Instead of querying taxable_class with new changes we can query on taxonomy itself.
    i.e. taxonomy.send("#{base_class.name.underscore}_ids")
    Please correct me if I misunderstood something here.

  • In taxonomy.rb,
    _ids methods are overwritten where ignore_types are returned by querying on type class instead of DB relations. For users, all with unscoped.except_admin.except_hidden and for other types, it simply returns all records.
    It may cause conflicts in above mentioned case and other areas.
    Please guide me on this.

Pending Work - I will add test cases once code changes done.

@theforeman-bot
Copy link
Member

@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:

    $ git pull --rebase upstream develop

This message was auto-generated by Foreman's prprocessor

@ares
Copy link
Member

ares commented Oct 10, 2019

@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.

@kgaikwad
Copy link
Member Author

@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.
Yeah, after 1.24 branching we can revisit this as I am not sure about how many changes will be required to fix this "all " options and what will be the impact on current behaviour.

@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 6c32e8b to 137a7b6 Compare December 21, 2019 09:59
@theforeman-bot theforeman-bot added Needs re-review Needs testing Legacy JS PRs making changes in the legacy Javascript stack and removed Waiting on contributor labels Dec 21, 2019
app/models/taxonomy.rb Outdated Show resolved Hide resolved
app/helpers/form_helper.rb Outdated Show resolved Hide resolved
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 137a7b6 to d74da02 Compare December 21, 2019 10:03
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from d74da02 to fe70805 Compare December 21, 2019 10:05
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 1e004f2 to 707b743 Compare December 23, 2019 12:52
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from 707b743 to bf5f00e Compare January 2, 2020 11:45
@kgaikwad
Copy link
Member Author

kgaikwad commented Jan 2, 2020

@ares,
I have done with rebase.
Tests are green. As I mentioned above, I would like to know if any additional migration or rake task changes are required so that it will run smoothly on any environment.

EDIT - I have tested with few general scenarios:

  • Tested with organization/location where select all option is enabled for few ignore_types.
  • Tested behavior of child organization/location when respective parent is having this "select all" option enabled against ignore_types.
  • DB changes as per UI modification while creating/updating records having organizations/locations with such options.
    If any other complex scenarios that I should test then please let me know.

@bkearney
Copy link
Contributor

bkearney commented Apr 2, 2020

@ares are we still waiting on the updates from @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".
@kgaikwad kgaikwad force-pushed the 6150-fix-select-all-users-for-taxonomy branch from bf5f00e to 3c45322 Compare April 2, 2020 15:01
@@ -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' ])

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.

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)

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.

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)

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.

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

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.

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)

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.

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)

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.


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)

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.

@kgaikwad
Copy link
Member Author

kgaikwad commented Apr 2, 2020

@bkearney, @ares,
Had conflicts as few days back fixed "Style/RedundantSelf cop" to master. Resolved and done with rebase.

@tbrisker
Copy link
Member

closing for now, thanks @kgaikwad !

@tbrisker tbrisker closed this Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Legacy JS PRs making changes in the legacy Javascript stack Needs re-review Needs testing Waiting on contributor
Projects
None yet
10 participants