Revision 3ebc95da
Added by Marek Hulán about 7 years ago
app/models/role.rb | ||
---|---|---|
compare = 'not' if args.first
|
||
where("#{compare} builtin = 0")
|
||
}
|
||
scope :cloned, -> { where.not(:cloned_from_id => nil) }
|
||
|
||
validates_lengths_from_database
|
||
|
||
... | ... | |
|
||
has_many :permissions, :through => :filters
|
||
|
||
has_many :cloned_roles, :class_name => 'Role', :foreign_key => 'cloned_from_id', :dependent => :nullify
|
||
belongs_to :cloned_from, :class_name => 'Role'
|
||
|
||
# these associations are not used by Taxonomix but serve as a pattern for role filters
|
||
# we intentionally don't include Taxonomix since roles are not taxable, we only need these relations
|
||
taxonomy_join_table = :taxable_taxonomies
|
||
... | ... | |
end
|
||
|
||
def clone(role_params = {})
|
||
new_role = self.deep_clone(:except => [:name, :builtin],
|
||
new_role = self.deep_clone(:except => [:name, :builtin],
|
||
:include => [:locations, :organizations, { :filters => :permissions }])
|
||
new_role.attributes = role_params
|
||
new_role.cloned_from_id = self.id
|
||
new_role
|
||
end
|
||
|
app/views/api/v2/roles/main.json.rabl | ||
---|---|---|
|
||
extends "api/v2/roles/base"
|
||
|
||
attributes :builtin, :created_at, :updated_at
|
||
attributes :builtin, :cloned_from_id, :created_at, :updated_at
|
app/views/roles/_form.html.erb | ||
---|---|---|
<div class="tab-content">
|
||
<div class="tab-pane active" id="primary">
|
||
<% if @role.persisted? && (show_location_tab? || show_organization_tab?) %>
|
||
<h5><%= _("Changes to %s will propagate to all inheriting filters") % org_loc_string(_('and')) %></h5>
|
||
<hr>
|
||
<h5><%= _("Changes to %s will propagate to all inheriting filters") % org_loc_string(_('and')) %></h5>
|
||
<% divider = '<hr>'.html_safe %>
|
||
<% end %>
|
||
|
||
<% if @role.cloned_from_id.present? && User.current.can?(:view_roles, @role.cloned_from) %>
|
||
<h5><%= _("This role has been cloned from role %s").html_safe % link_to(@role.cloned_from.name, edit_role_path(@role.cloned_from)) %></h5>
|
||
<% divider = '<hr>'.html_safe %>
|
||
<% end %>
|
||
|
||
<%= divider %>
|
||
|
||
<%= text_f f, :name, :class => @role.builtin? ? "disabled" : "" %>
|
||
<%= textarea_f f, :description, :rows=> 5, :size => "col-md-4" %>
|
||
|
||
<%= hidden_field_tag :original_role_id, @original_role_id if @cloned_role %>
|
||
|
||
<% tax_help = N_("When the role's associated %{taxonomies} are changed,<br> the change will propagate to all inheriting filters.
|
db/migrate/20170228134258_add_clone_info_to_role.rb | ||
---|---|---|
class AddCloneInfoToRole < ActiveRecord::Migration
|
||
def change
|
||
add_column :roles, :cloned_from_id, :integer
|
||
end
|
||
end
|
test/models/role_test.rb | ||
---|---|---|
end
|
||
end
|
||
|
||
describe "Cloning" do
|
||
let(:role) { FactoryGirl.create(:role) }
|
||
let(:cloned_role) do
|
||
cloned_role = role.clone
|
||
cloned_role.name = "Clone of #{role.name}"
|
||
cloned_role.save!
|
||
cloned_role
|
||
end
|
||
|
||
it "cloned role keeps link to origin" do
|
||
assert_equal role, cloned_role.cloned_from
|
||
end
|
||
|
||
it "allows me to find all roles that were cloned from origin" do
|
||
another_role = FactoryGirl.create(:role)
|
||
cloned_role # enforce lazy let to create the cloned role and role
|
||
clones = role.cloned_roles
|
||
assert_include clones, cloned_role
|
||
assert_not_include clones, another_role
|
||
end
|
||
|
||
it 'nullifies the relation when origin is deleted' do
|
||
cloned_role # enforce lazy let to create the cloned role and role
|
||
assert role.destroy
|
||
cloned_role.reload
|
||
assert_nil cloned_role.cloned_from
|
||
end
|
||
|
||
it 'can be found by cloned scope' do
|
||
cloned_role # enforce lazy let to create the cloned role and role
|
||
assert_include Role.cloned, cloned_role
|
||
assert_not_include Role.cloned, role
|
||
end
|
||
end
|
||
|
||
context "System roles" do
|
||
should "return the default role" do
|
||
role = Role.default
|
Also available in: Unified diff
Fixes #18767 - keep cloning info