Skip to content

Commit e01de87

Browse files
committed
Register custom fields for inplace editing and add more field types
1 parent 85e9e6e commit e01de87

File tree

13 files changed

+468
-48
lines changed

13 files changed

+468
-48
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 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+
54+
def call
55+
form.check_box name: attribute,
56+
data: { controller: "inplace-edit",
57+
action: "click->inplace-edit#submitForm" },
58+
**@system_arguments
59+
end
60+
61+
private
62+
63+
def submit_url
64+
inplace_edit_field_submit_path(
65+
model: model.class.name,
66+
id: model.id,
67+
attribute:,
68+
system_arguments_json: @system_arguments.to_json
69+
)
70+
end
71+
end
72+
end
73+
end
74+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 CalculatedValueInputComponent < InplaceEditFields::TextInputComponent
35+
def initialize(form:, attribute:, model:, **system_arguments)
36+
system_arguments[:readonly] = true
37+
super
38+
end
39+
end
40+
end
41+
end
42+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 DateInputComponent < InplaceEditFields::TextInputComponent
35+
def initialize(form:, attribute:, model:, **system_arguments)
36+
system_arguments[:type] = :date
37+
super
38+
end
39+
end
40+
end
41+
end
42+
end

app/components/open_project/common/inplace_edit_fields/display_fields/display_field_component.rb

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Common
3333
module InplaceEditFields
3434
module DisplayFields
3535
class DisplayFieldComponent < ViewComponent::Base
36-
include OpenProject::TextFormatting
36+
include OpPrimer::ComponentHelpers
3737

3838
attr_reader :model, :attribute, :writable
3939

@@ -45,28 +45,67 @@ def initialize(model:, attribute:, writable:, **system_arguments)
4545
@system_arguments = system_arguments
4646
end
4747

48+
def render_label
49+
# Check if this is a custom field attribute
50+
if attribute.to_s.start_with?("custom_field_")
51+
custom_field_id = attribute.to_s.sub("custom_field_", "").to_i
52+
custom_field = CustomField.find_by(id: custom_field_id)
53+
return custom_field.name if custom_field
54+
end
55+
56+
label = attribute.to_s.humanize
57+
label = label.titleize if attribute.to_s.include?("_")
58+
label
59+
end
60+
4861
def render_display_value
4962
value = model.public_send(attribute)
5063

51-
if value.present?
52-
format_text(value)
64+
if value.is_a?(TrueClass) || value.is_a?(FalseClass)
65+
boolean_display_value(value)
66+
elsif value.present?
67+
value.to_s
5368
else
5469
"–"
5570
end
5671
end
5772

5873
def display_field_arguments
5974
@display_field_arguments ||= {
60-
classes: "op-inplace-edit--display-field #{'op-inplace-edit--display-field_editable' if writable}",
75+
classes: "op-inplace-edit--display-field #{'op-inplace-edit--display-field_editable' if writable?}",
6176
data: {
6277
controller: "inplace-edit",
6378
inplace_edit_url_value: edit_url,
64-
action: writable ? "click->inplace-edit#request" : ""
79+
action: writable? ? "click->inplace-edit#request" : ""
6580
}
6681
}
6782
end
6883

6984
def call
85+
flex_layout do |flex|
86+
flex.with_row(mb: 1) do
87+
render(Primer::BaseComponent.new(tag: :label, hidden: @system_arguments[:visually_hide_label])) do
88+
render_label
89+
end
90+
end
91+
92+
flex.with_row do
93+
input_specific_call
94+
end
95+
96+
flex.with_row do
97+
render OpenProject::Common::AttributeHelpTextCaptionComponent.new(
98+
help_text: helpers.help_text_for(
99+
model,
100+
attribute,
101+
current_user: helpers.current_user
102+
)
103+
)
104+
end
105+
end
106+
end
107+
108+
def input_specific_call
70109
render(Primer::BaseComponent.new(tag: :div, **display_field_arguments)) do
71110
render_display_value
72111
end
@@ -82,6 +121,14 @@ def edit_url
82121
system_arguments_json: @system_arguments.to_json
83122
)
84123
end
124+
125+
def boolean_display_value(value)
126+
I18n.t("general_text_#{value ? 'yes' : 'no'}")
127+
end
128+
129+
def writable?
130+
writable && @system_arguments[:readonly] == false
131+
end
85132
end
86133
end
87134
end

app/components/open_project/common/inplace_edit_fields/display_fields/display_fields_component.sass

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
.op-inplace-edit
22
&--display-field
33
&_editable
4-
margin-left: -9px !important // cancel out 8px padding + 1px border
5-
margin-right: -9px !important // cancel out 8px padding + 1px border
6-
padding: var(--base-size-8)
7-
width: calc(100% + 18px) !important
4+
padding: var(--base-size-4) var(--base-size-8)
85
border: 1px solid transparent
96
border-radius: var(--borderRadius-medium)
107

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@ module Common
3333
module InplaceEditFields
3434
module DisplayFields
3535
class RichTextAreaComponent < DisplayFieldComponent
36+
include OpenProject::TextFormatting
37+
3638
attr_reader :model, :attribute, :writable
3739

38-
def call
40+
def input_specific_call
3941
render(Primer::BaseComponent.new(tag: :div, **display_field_arguments)) do
4042
render(Primer::BaseComponent.new(tag: :div,
4143
classes: "op-uc-container op-uc-container_reduced-headings -multiline")) do
4244
render_display_value
4345
end
4446
end
4547
end
48+
49+
def render_display_value
50+
value = model.public_send(attribute)
51+
52+
if value.present?
53+
format_text(value)
54+
else
55+
"–"
56+
end
57+
end
4658
end
4759
end
4860
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 FloatInputComponent < InplaceEditFields::TextInputComponent
35+
def initialize(form:, attribute:, model:, **system_arguments)
36+
system_arguments[:type] = :number
37+
system_arguments[:step] = "any"
38+
super
39+
end
40+
end
41+
end
42+
end
43+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 IntegerInputComponent < InplaceEditFields::TextInputComponent
35+
def initialize(form:, attribute:, model:, **system_arguments)
36+
system_arguments[:type] = :number
37+
super
38+
end
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)