Skip to content

Commit d3c2ce9

Browse files
author
root
committed
Convert serialized field to has_many relation
1 parent 54a5767 commit d3c2ce9

11 files changed

+79
-51
lines changed

Diff for: app/controllers/repository_protected_branches_controller.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def index
1414

1515

1616
def new
17-
@protected_branch = @repository.protected_branches.new()
18-
@protected_branch.user_list = []
17+
@protected_branch = @repository.protected_branches.new
1918
end
2019

2120

Diff for: app/helpers/repository_protected_branches_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module RepositoryProtectedBranchesHelper
2+
3+
def protected_branch_available_users
4+
@project.member_principals.map(&:user).uniq.sort
5+
end
6+
7+
end

Diff for: app/models/protected_branches_user.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class ProtectedBranchesUser < ActiveRecord::Base
2+
unloadable
3+
4+
## Relations
5+
belongs_to :protected_branch, class_name: 'RepositoryProtectedBranche'
6+
belongs_to :user
7+
end

Diff for: app/models/repository_protected_branche.rb

+4-25
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,21 @@ class RepositoryProtectedBranche < ActiveRecord::Base
77
acts_as_list
88

99
## Attributes
10-
attr_accessible :path, :permissions, :position, :user_list
10+
attr_accessible :path, :permissions, :position, :user_ids
1111

1212
## Relations
1313
belongs_to :repository
14+
has_many :protected_branches_users, foreign_key: :protected_branch_id, dependent: :destroy
15+
has_many :users, through: :protected_branches_users
1416

1517
## Validations
1618
validates :repository_id, presence: true
1719
validates :path, presence: true, uniqueness: { scope: [:permissions, :repository_id] }
1820
validates :permissions, presence: true, inclusion: { in: VALID_PERMS }
19-
validates :user_list, presence: true
20-
21-
## Serializations
22-
serialize :user_list, Array
23-
24-
## Callbacks
25-
before_validation :remove_blank_items
2621

2722
## Scopes
2823
default_scope { order('position ASC') }
2924

30-
## Delegation
31-
delegate :project, to: :repository
32-
3325

3426
class << self
3527

@@ -44,21 +36,8 @@ def clone_from(parent)
4436
end
4537

4638

47-
def available_users
48-
project.member_principals.map(&:user).compact.uniq.map{ |u| u.login }.sort
49-
end
50-
51-
5239
def allowed_users
53-
user_list.map{|u| User.find_by_login(u).gitolite_identifier}.sort
40+
users.map { |u| u.gitolite_identifier }.sort
5441
end
5542

56-
57-
private
58-
59-
60-
def remove_blank_items
61-
self.user_list = user_list.select{|u| !u.blank?} rescue []
62-
end
63-
6443
end

Diff for: app/views/repository_protected_branches/_form.html.haml

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
%p= f.text_field :path, required: true, size: 65, label: l(:label_branch_path)
55
%p= f.select :permissions, options_for_select(RepositoryProtectedBranche::VALID_PERMS, @protected_branch.permissions), required: true, label: :label_permissions
66

7-
= hidden_field_tag "repository_protected_branche[user_list][]", ""
7+
= hidden_field_tag 'repository_protected_branche[user_ids][]', ''
88

99
%p
10-
%label{ for: "repository_protected_branche[user_list]" }= l(:label_user_list) + ' :'
10+
%label{ for: 'repository_protected_branche[user_ids]' }= l(:label_user_list) + ' :'
1111

12-
= tag_it_list 'repository_protected_branche_user_list',
13-
{ name: 'repository_protected_branche[user_list][]' },
14-
{ source: 'user_list', placeholder: '+ add user' } do
15-
- if @protected_branch.user_list.any?
16-
- @protected_branch.user_list.each do |item|
17-
%li= item
18-
19-
:javascript
20-
var user_list = #{raw @protected_branch.available_users.to_json};
21-
$(document).ready(function(){ setTagIt(); });
12+
- protected_branch_available_users.each do |user|
13+
%p
14+
%label{ style: 'margin-left: 0; width: auto; font-weight: lighter;' }
15+
= check_box_tag 'repository_protected_branche[user_ids][]', user.id, @protected_branch.users.include?(user)
16+
= user
17+
%br

Diff for: app/views/repository_protected_branches/index.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
%td{ style: 'font-family: Consolas;' }= protected_branch.path
3636

3737
%td
38-
- protected_branch.user_list.each do |user|
38+
- protected_branch.users.each do |user|
3939
%span{ class: 'label label-info' }= user
4040

4141
%td{ class: 'buttons' }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class CreateProtectedBranchesUsers < ActiveRecord::Migration
2+
3+
def self.up
4+
create_table :protected_branches_users do |t|
5+
t.column :protected_branch_id, :integer
6+
t.column :user_id, :integer
7+
end
8+
9+
add_index :protected_branches_users, [:protected_branch_id, :user_id], unique: true, name: 'unique__protected_branch_id_user_id'
10+
end
11+
12+
def self.down
13+
drop_table :protected_branches_users
14+
end
15+
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class RepositoryProtectedBrancheWrapped < RepositoryProtectedBranche
2+
serialize :user_list, Array
3+
end
4+
5+
class MigrateProtectedBranchesUsers < ActiveRecord::Migration
6+
7+
def self.up
8+
RepositoryProtectedBrancheWrapped.all.each do |protected_branch|
9+
users = protected_branch.user_list.map { |u| User.find_by_login(u) }.compact.uniq
10+
protected_branch.users << users
11+
end
12+
remove_column :repository_protected_branches, :user_list
13+
end
14+
15+
def self.down
16+
add_column :repository_protected_branches, :user_list, :text, after: :permissions
17+
RepositoryProtectedBrancheWrapped.all.each do |protected_branch|
18+
users = protected_branch.users.map { |u| u.login }.compact.uniq
19+
protected_branch.user_list = users
20+
protected_branch.save!
21+
end
22+
end
23+
24+
end

Diff for: lib/redmine_git_hosting/patches/user_patch.rb

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def self.included(base)
1414

1515
# Relations
1616
has_many :gitolite_public_keys, dependent: :destroy
17+
has_many :protected_branches_users, dependent: :destroy
18+
has_many :protected_branches, through: :protected_branches_users
1719

1820
# Callbacks
1921
after_save :check_if_status_changed

Diff for: spec/models/repository_protected_branche_spec.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def build_protected_branch(opts = {})
99

1010
describe "Valid RepositoryProtectedBranche creation" do
1111
before(:each) do
12-
@protected_branch = build_protected_branch(:path => 'devel', :permissions => 'RW', :user_list => %w(user1 user2))
12+
@protected_branch = build_protected_branch(path: 'devel', permissions: 'RW')
1313
end
1414

1515
subject { @protected_branch }
@@ -18,24 +18,22 @@ def build_protected_branch(opts = {})
1818
it { should allow_mass_assignment_of(:path) }
1919
it { should allow_mass_assignment_of(:permissions) }
2020
it { should allow_mass_assignment_of(:position) }
21-
it { should allow_mass_assignment_of(:user_list) }
21+
it { should allow_mass_assignment_of(:user_ids) }
2222

2323
## Relations
2424
it { should belong_to(:repository) }
25+
it { should have_many(:protected_branches_users).with_foreign_key(:protected_branch_id).dependent(:destroy) }
26+
it { should have_many(:users).through(:protected_branches_users) }
2527

2628
## Validations
2729
it { should be_valid }
2830

2931
it { should validate_presence_of(:repository_id) }
3032
it { should validate_presence_of(:path) }
3133
it { should validate_presence_of(:permissions) }
32-
it { should validate_presence_of(:user_list) }
3334

3435
it { should validate_uniqueness_of(:path).scoped_to([:permissions, :repository_id]) }
3536

3637
it { should validate_inclusion_of(:permissions).in_array(%w(RW RW+)) }
37-
38-
## Serializations
39-
it { should serialize(:user_list) }
4038
end
4139
end

Diff for: spec/models/user_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
describe User do
44

55
before(:each) do
6-
@user = create(:user)
6+
@user = build(:user)
77
end
88

99
subject { @user }
1010

11-
it { should be_valid }
12-
13-
it { should respond_to(:gitolite_identifier) }
11+
it { should have_many(:protected_branches_users).dependent(:destroy) }
12+
it { should have_many(:protected_branches).through(:protected_branches_users) }
1413

1514
it "has a gitolite_identifier" do
16-
expect(@user.gitolite_identifier).to match(/redmine_user\d+_\d+/)
15+
user = create(:user)
16+
expect(user.gitolite_identifier).to match(/redmine_user\d+_\d+/)
1717
end
1818
end

0 commit comments

Comments
 (0)