Skip to content

Commit 2558874

Browse files
committed
Add BaseClass for input edit fields to avoid duplication
1 parent a3ec1fc commit 2558874

File tree

8 files changed

+69
-79
lines changed

8 files changed

+69
-79
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module OpenProject
32+
module Common
33+
module InplaceEditFields
34+
class BaseFieldComponent < ViewComponent::Base
35+
attr_reader :form, :attribute, :model, :show_action_buttons
36+
37+
def self.display_class
38+
DisplayFields::DisplayFieldComponent
39+
end
40+
41+
def initialize(form:, attribute:, model:, show_action_buttons: true, **system_arguments)
42+
super()
43+
@form = form
44+
@attribute = attribute
45+
@model = model
46+
@show_action_buttons = show_action_buttons
47+
@system_arguments = system_arguments
48+
end
49+
50+
def custom_field?
51+
attribute.to_s.start_with?("custom_field_")
52+
end
53+
54+
def custom_field
55+
return @custom_field if defined?(@custom_field)
56+
57+
@custom_field = CustomField.find_by(id: attribute.to_s.sub("custom_field_", "").to_i)
58+
end
59+
end
60+
end
61+
end
62+
end

app/components/open_project/common/inplace_edit_fields/boolean_input_component.rb

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,7 @@
3131
module OpenProject
3232
module Common
3333
module InplaceEditFields
34-
class BooleanInputComponent < ViewComponent::Base
35-
attr_reader :form, :attribute, :model
36-
37-
def self.display_class
38-
DisplayFields::DisplayFieldComponent
39-
end
40-
41-
def initialize(form:, attribute:, model:, **system_arguments)
42-
super()
43-
@form = form
44-
@attribute = attribute
45-
@model = model
46-
@system_arguments = system_arguments
47-
@system_arguments[:classes] = class_names(
48-
@system_arguments[:classes],
49-
"op-inplace-edit-field--boolean"
50-
)
51-
@system_arguments[:label] ||= model.class.human_attribute_name(attribute)
52-
end
53-
34+
class BooleanInputComponent < BaseFieldComponent
5435
def call
5536
form.check_box name: attribute,
5637
data: { controller: "inplace-edit",

app/components/open_project/common/inplace_edit_fields/link_input_component.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ module OpenProject
3232
module Common
3333
module InplaceEditFields
3434
class LinkInputComponent < TextInputComponent
35-
attr_reader :form, :attribute, :model
36-
3735
def self.display_class
3836
DisplayFields::LinkInputComponent
3937
end

app/components/open_project/common/inplace_edit_fields/rich_text_area_component.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@
3131
module OpenProject
3232
module Common
3333
module InplaceEditFields
34-
class RichTextAreaComponent < ViewComponent::Base
35-
attr_reader :form, :attribute, :model, :show_action_buttons
36-
34+
class RichTextAreaComponent < BaseFieldComponent
3735
def self.display_class
3836
DisplayFields::RichTextAreaComponent
3937
end
4038

4139
def initialize(form:, attribute:, model:, show_action_buttons: true, **system_arguments)
42-
super()
43-
@form = form
44-
@attribute = attribute
45-
@model = model
46-
@show_action_buttons = show_action_buttons
47-
@system_arguments = system_arguments
40+
super
4841
@system_arguments[:classes] = class_names(
4942
@system_arguments[:classes],
5043
"op-inplace-edit-field--text-area"

app/components/open_project/common/inplace_edit_fields/select_list_component.rb

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,18 @@
3131
module OpenProject
3232
module Common
3333
module InplaceEditFields
34-
class SelectListComponent < ViewComponent::Base
35-
attr_reader :form, :attribute, :model
36-
34+
class SelectListComponent < BaseFieldComponent
3735
def self.display_class
3836
DisplayFields::SelectListComponent
3937
end
4038

4139
def initialize(form:, attribute:, model:, show_action_buttons: true, **system_arguments)
42-
super()
43-
@form = form
44-
@attribute = attribute
45-
@model = model
46-
@show_action_buttons = show_action_buttons
47-
@system_arguments = system_arguments
48-
@system_arguments[:classes] = class_names(
49-
@system_arguments[:classes],
50-
"op-inplace-edit-field--select-list"
51-
)
52-
@system_arguments[:label] ||= model.class.human_attribute_name(attribute)
40+
super
5341

5442
@system_arguments[:autocomplete_options] ||= {}
5543
@system_arguments[:autocomplete_options][:model] ||= { id: model.id, name: model.name }
5644
@system_arguments[:autocomplete_options][:inputName] ||= attribute
57-
@system_arguments[:autocomplete_options][:wrapper_id] ||= system_arguments[:wrapper_id]
45+
@system_arguments[:autocomplete_options][:wrapper_id] ||= @system_arguments[:wrapper_id]
5846
if @system_arguments[:autocomplete_options][:focusDirectly].nil?
5947
@system_arguments[:autocomplete_options][:focusDirectly] =
6048
true
@@ -105,16 +93,6 @@ def render_custom_field_input
10593
def render_autocompleter
10694
form.autocompleter(name: attribute, **@system_arguments)
10795
end
108-
109-
def custom_field?
110-
attribute.to_s.start_with?("custom_field_")
111-
end
112-
113-
def custom_field
114-
return @custom_field if defined?(@custom_field)
115-
116-
@custom_field = CustomField.find_by(id: attribute.to_s.sub("custom_field_", "").to_i)
117-
end
11896
end
11997
end
12098
end

app/components/open_project/common/inplace_edit_fields/text_input_component.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,7 @@
3131
module OpenProject
3232
module Common
3333
module InplaceEditFields
34-
class TextInputComponent < ViewComponent::Base
35-
attr_reader :form, :attribute, :model
36-
37-
def self.display_class
38-
DisplayFields::DisplayFieldComponent
39-
end
40-
41-
def initialize(form:, attribute:, model:, **system_arguments)
42-
super()
43-
@form = form
44-
@attribute = attribute
45-
@model = model
46-
@system_arguments = system_arguments
47-
end
48-
34+
class TextInputComponent < BaseFieldComponent
4935
def call
5036
form.text_field name: attribute,
5137
data: { controller: "inplace-edit",

app/components/open_project/common/inplace_edit_fields/user_select_list_component.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,10 @@ module OpenProject
3232
module Common
3333
module InplaceEditFields
3434
class UserSelectListComponent < SelectListComponent
35-
attr_reader :form, :attribute, :model
36-
3735
def self.display_class
3836
DisplayFields::UserSelectListComponent
3937
end
4038

41-
def initialize(form:, attribute:, model:, **system_arguments)
42-
super
43-
end
44-
4539
private
4640

4741
def render_custom_field_input

app/components/open_project/common/inplace_edit_fields/version_select_list_component.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ module OpenProject
3232
module Common
3333
module InplaceEditFields
3434
class VersionSelectListComponent < SelectListComponent
35-
attr_reader :form, :attribute, :model
36-
3735
def initialize(form:, attribute:, model:, **system_arguments)
3836
super
3937

0 commit comments

Comments
 (0)