Skip to content

Commit a3e05a8

Browse files
Extend make visible in stats (#2168)
* used scope to ensure that Moods set to not_visible are not tag-able in Moments * tests for mood visibility in moments * categories and strategy set to visible: false are not shown in dropdown and therefore cannot be selected * changed label with translations * showing "Not visible to tag and display in profile stats" when element is not visible, included translations * reflect changes to visible_helper in spec * rendering icons based on mood visibility * rendered icons conditionally according according to visibility status where applicable * fix rubocop offenses * yarn lint run to solve circle cI --fix * corrected failure test on .storyActionsViewers visiblity problem * added conditional to viewers helper to account for when an element does not respond to the :viewers method * add skipped translation to de.yml * applied suggested changes to DRY the changes made to visibility based methods * when a visibility key (vi sible, not_visible) resolves to false, it will be removed from the returned hash so as not to cause a Type error in the StoryAction.jsx * associated elements are displayed on edit regardless of visiblity status * test to ensure edited elements retain association on edit * rixed typo `reders => renders * Moment and Strategy is unable to remove associations when no params is passed un update * removed redundant model methods and accompanying tests * Moment and Strategy are now able to remove associations when no params is passed to the controller by a before_save callback * update nokogiri to 1.13.9 pass circle ci * removed left over comments from code
1 parent 7b9bf25 commit a3e05a8

35 files changed

+220
-200
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ GEM
241241
ruby2_keywords (~> 0.0.1)
242242
netrc (0.11.0)
243243
nio4r (2.5.8)
244-
nokogiri (1.13.8)
244+
nokogiri (1.13.9)
245245
mini_portile2 (~> 2.8.0)
246246
racc (~> 1.4)
247247
oauth2 (1.4.9)

app/controllers/moments_controller.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ def moment_params
109109
end
110110

111111
def set_association_variables!
112-
@categories = current_user.categories.order(created_at: :desc)
112+
@categories = current_user.categories.is_visible
113+
.or(Category.where(id: @moment.category_ids))
114+
.order(created_at: :desc)
113115
@category = Category.new
114-
@moods = current_user.moods.order(created_at: :desc)
116+
@moods = current_user.moods.is_visible.or(Mood.where(id: @moment.mood_ids))
117+
.order(created_at: :desc)
115118
@mood = Mood.new
116119
@strategies = associated_strategies
117120
@strategy = Strategy.new
@@ -127,6 +130,8 @@ def associated_strategies
127130
Strategy.where(user: @viewers).each do |strategy|
128131
strategy_ids << strategy.id if strategy.viewer?(current_user)
129132
end
130-
Strategy.where(id: strategy_ids).order(created_at: :desc)
133+
Strategy.is_visible.where(id: strategy_ids)
134+
.or(Strategy.where(id: @moment.strategy_ids))
135+
.order(created_at: :desc)
131136
end
132137
end

app/controllers/strategies_controller.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def quick_create
4141
def new
4242
@viewers = current_user.allies_by_status(:accepted)
4343
@strategy = Strategy.new
44-
@categories = current_user.categories.order('created_at DESC')
44+
@categories = current_user.categories.is_visible.order('created_at DESC')
4545
@category = Category.new
4646
@strategy.build_perform_strategy_reminder
4747
end
@@ -52,7 +52,9 @@ def edit
5252
redirect_to_path(strategy_path(@strategy))
5353
end
5454
@viewers = current_user.allies_by_status(:accepted)
55-
@categories = current_user.categories.order('created_at DESC')
55+
@categories = current_user.categories.is_visible
56+
.or(Category.where(id: @strategy.category_ids))
57+
.order('created_at DESC')
5658
@category = Category.new
5759
PerformStrategyReminder.find_or_initialize_by(strategy_id: @strategy.id)
5860
end

app/helpers/categories_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def actions_setup(element, url_helper)
4242
link: link
4343
},
4444
delete: action_delete(url_helper),
45-
visible: get_visible(element.visible)
45+
not_visible: !element.visible && get_visible(element.visible),
46+
visible: element.visible && get_visible(element.visible)
4647
}
4748
end
4849

app/helpers/moments_helper.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def present_moment_or_strategy(element)
6363
{ name: element.name,
6464
link: present_object[:url_helper],
6565
date: TimeAgo.created_or_edited(element),
66-
actions: moment_or_strategy_actions(element, present_object),
66+
actions: element_actions(element, present_object),
6767
draft: !element.published? ? t('draft') : nil,
6868
categories: element.category_names_and_slugs,
6969
moods: present_object[:moods],
@@ -86,13 +86,20 @@ def get_resources_data(moment, current_user)
8686

8787
private
8888

89-
def moment_or_strategy_actions(element, present_object)
89+
def element_actions(element, present_object)
9090
actions = user_actions(element,
9191
present_object,
9292
element.user_id == current_user&.id)
93-
{ edit: actions[:edit], delete: actions[:delete],
94-
viewers: actions[:viewers], visible: actions[:visible],
95-
share_link_info: actions[:share_link_info] }
93+
{
94+
edit: actions[:edit],
95+
delete: actions[:delete],
96+
viewers: actions[:viewers],
97+
not_visible: (!element.is_a?(Moment) &&
98+
!element.visible) &&
99+
actions[:visible],
100+
visible: (element.is_a?(Moment) || element.visible) && actions[:visible],
101+
share_link_info: actions[:share_link_info]
102+
}.delete_if { |_, value| value == false }
96103
end
97104

98105
def story_by(element)

app/helpers/viewers_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@ def get_viewers_for(data, data_type)
2222
result.uniq
2323
end
2424

25+
def element_visibility_based_props(element)
26+
{
27+
dark: true,
28+
actions: {
29+
delete: {
30+
name: t('common.actions.delete'),
31+
link: url_for(element),
32+
dataConfirm: t('common.actions.confirm'),
33+
dataMethod: 'delete'
34+
},
35+
edit: {
36+
name: t('common.actions.edit'),
37+
link: url_for([:edit, element])
38+
},
39+
not_visible: !element.visible && get_visible(element.visible),
40+
viewers: element.respond_to?(:viewers) &&
41+
get_viewer_list(element.viewers, nil),
42+
visible: element.visible && get_visible(element.visible)
43+
}.delete_if { |_, value| value == false },
44+
storyName: element.name
45+
}
46+
end
47+
2548
private
2649

2750
def get_link(link)

app/helpers/visible_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
module VisibleHelper
33
def get_visible(visible)
44
return t('shared.stats.visible_in_stats') if visible
5+
6+
t('shared.stats.not_visible_in_stats')
57
end
68
end

app/models/category.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515

1616
class Category < ApplicationRecord
17+
include IsVisibleConcern
1718
extend FriendlyId
1819

1920
friendly_id :name

app/models/concerns/common_methods.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ def category_names_and_slugs
1414
names_and_slugs_hash(categories.pluck(:name, :slug), 'categories')
1515
end
1616

17+
def elements_array_data(elements)
18+
elements.each do |element|
19+
associated_elements = element.pluralize
20+
klass = element.capitalize.constantize
21+
if send(element).is_a?(Array)
22+
element_ids = send(element).collect(&:to_i)
23+
send("#{associated_elements}=",
24+
klass.where(user_id: user_id, id: element_ids))
25+
else
26+
send("#{associated_elements}=", klass.none)
27+
end
28+
end
29+
end
30+
1731
private
1832

1933
def names_and_slugs_hash(data, model_name)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
module IsVisibleConcern
3+
extend ActiveSupport::Concern
4+
5+
included do
6+
scope :is_visible, -> { where(visible: true) }
7+
end
8+
end

app/models/moment.rb

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class Moment < ApplicationRecord
4545
friendly_id :name
4646
serialize :viewers, Array
4747

48-
before_save :category_array_data
48+
before_save do
49+
elements_array_data(%w[category mood strategy])
50+
end
4951
before_save :viewers_array_data
50-
before_save :mood_array_data
51-
before_save :strategy_array_data
5252

5353
belongs_to :user
5454

@@ -80,31 +80,10 @@ def self.find_secret_share!(identifier)
8080
)
8181
end
8282

83-
def category_array_data
84-
return unless category.is_a?(Array)
85-
86-
category_ids = category.collect(&:to_i)
87-
self.categories = Category.where(user_id: user_id, id: category_ids)
88-
end
89-
9083
def viewers_array_data
9184
self.viewers = viewers.collect(&:to_i) if viewers.is_a?(Array)
9285
end
9386

94-
def mood_array_data
95-
return unless mood.is_a?(Array)
96-
97-
mood_ids = mood.collect(&:to_i)
98-
self.moods = Mood.where(user_id: user_id, id: mood_ids)
99-
end
100-
101-
def strategy_array_data
102-
return unless strategy.is_a?(Array)
103-
104-
strategy_ids = strategy.collect(&:to_i)
105-
self.strategies = Strategy.where(user_id: user_id, id: strategy_ids)
106-
end
107-
10887
def owned_by?(user)
10988
user&.id == user_id
11089
end

app/models/mood.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#
1515

1616
class Mood < ApplicationRecord
17+
include IsVisibleConcern
18+
1719
USER_DATA_ATTRIBUTES = %w[
1820
id
1921
name

app/models/strategy.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#
1919

2020
class Strategy < ApplicationRecord
21+
include IsVisibleConcern
2122
include Viewer
2223
include CommonMethods
2324
extend FriendlyId
@@ -39,7 +40,10 @@ class Strategy < ApplicationRecord
3940
friendly_id :name
4041
serialize :viewers, Array
4142

42-
before_save :category_array_data
43+
before_save do
44+
elements_array_data(['category'])
45+
end
46+
4347
before_save :viewers_array_data
4448

4549
belongs_to :user
@@ -70,13 +74,6 @@ def viewers_array_data
7074
self.viewers = viewers.collect(&:to_i) if viewers.is_a?(Array)
7175
end
7276

73-
def category_array_data
74-
return unless category.is_a?(Array)
75-
76-
category_ids = category.collect(&:to_i)
77-
self.categories = Category.where(user_id: user_id, id: category_ids)
78-
end
79-
8077
def published?
8178
published_at.present?
8279
end

app/views/categories/show.html.erb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,7 @@
1212
<div class="gridItemBoxDark gridRowSpaceBetween smallMarginTop">
1313
<div class="gridRowSpaceBetween">
1414
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
15-
<%= react_component('StoryActions', props: {
16-
dark: true,
17-
actions: {
18-
delete: {
19-
name: t('common.actions.delete'),
20-
link: url_for(@category),
21-
dataConfirm: t('common.actions.confirm'),
22-
dataMethod: 'delete'
23-
},
24-
edit: {
25-
name: t('common.actions.edit'),
26-
link: edit_category_path(@category)
27-
},
28-
visible: get_visible(@category.visible)
29-
},
30-
storyName: @category.name
31-
}) %>
15+
<%= react_component('StoryActions', props: element_visibility_based_props(@category))%>
3216
</div>
3317
</div>
3418
<%= render partial: '/tag_usage/index' %>

app/views/moods/show.html.erb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,7 @@
1212
<div class="gridItemBoxDark gridRowSpaceBetween smallMarginTop">
1313
<div class="gridRowSpaceBetween">
1414
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
15-
<%= react_component('StoryActions', props: {
16-
dark: true,
17-
actions: {
18-
delete: {
19-
name: t('common.actions.delete'),
20-
link: url_for(@mood),
21-
dataConfirm: t('common.actions.confirm'),
22-
dataMethod: 'delete'
23-
},
24-
edit: {
25-
name: t('common.actions.edit'),
26-
link: edit_mood_path(@mood)
27-
},
28-
visible: get_visible(@mood.visible)
29-
},
30-
storyName: @mood.name
31-
}) %>
15+
<%= react_component('StoryActions', props: element_visibility_based_props(@mood)) %>
3216
</div>
3317
</div>
3418
<%= render partial: '/tag_usage/index' %>

app/views/strategies/show.html.erb

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,7 @@
2626
<div class="gridRowSpaceBetween">
2727
<div class="gridRowSpaceBetween">
2828
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
29-
<%= react_component('StoryActions', props: {
30-
dark: true,
31-
actions: {
32-
edit: {
33-
link: edit_strategy_path(@strategy),
34-
name: t('common.actions.edit')
35-
},
36-
delete: {
37-
name: t('common.actions.delete'),
38-
link: url_for(@strategy),
39-
dataMethod: 'delete',
40-
dataConfirm: t('common.actions.confirm')
41-
},
42-
viewers: get_viewer_list(@strategy.viewers, nil),
43-
visible: get_visible(@strategy.visible)
44-
},
45-
storyName: @strategy.name
46-
}) %>
29+
<%= react_component('StoryActions', props: element_visibility_based_props(@strategy)) %>
4730
</div>
4831
<%= print_reminders(@strategy) %>
4932
</div>

client/app/components/Story/StoryActions.jsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
faDoorOpen,
1111
faDoorClosed,
1212
faExclamationTriangle,
13+
faEyeSlash,
1314
faCalendarPlus,
1415
faCalendarMinus,
1516
faChartLine,
@@ -37,6 +38,7 @@ export type Actions = {
3738
remove_from_google_cal?: Action,
3839
viewers?: string,
3940
visible?: string,
41+
not_visible?: string,
4042
};
4143

4244
export type Props = {
@@ -53,6 +55,7 @@ const LEAVE = 'leave';
5355
const REPORT = 'report';
5456
const VIEWERS = 'viewers';
5557
const VISIBLE = 'visible';
58+
const NOT_VISIBLE = 'not_visible';
5659
const ADD_TO_G_CAL = 'add_to_google_cal';
5760
const REMOVE_FROM_G_CAL = 'remove_from_google_cal';
5861
const SHARE_LINK_INFO = 'share_link_info';
@@ -83,6 +86,14 @@ const classMap = (dark: ?boolean) => {
8386
aria-label={I18n.t('shared.stats.visible_in_stats')}
8487
/>
8588
),
89+
not_visible: (
90+
<FontAwesomeIcon
91+
icon={faEyeSlash}
92+
className={className}
93+
tabIndex={0}
94+
aria-label={I18n.t('shared.stats.not_visible_in_stats')}
95+
/>
96+
),
8697
add_to_google_cal: (
8798
<FontAwesomeIcon icon={faCalendarPlus} className={className} />
8899
),
@@ -165,7 +176,12 @@ const displayItem = (
165176
hasStory: ?boolean,
166177
dark: ?boolean,
167178
) => {
168-
if (item === VIEWERS || item === VISIBLE || item === SHARE_LINK_INFO) {
179+
if (
180+
item === VIEWERS
181+
|| item === VISIBLE
182+
|| item === NOT_VISIBLE
183+
|| item === SHARE_LINK_INFO
184+
) {
169185
return displayNonLink(actions, item, hasStory, dark);
170186
}
171187
return displayLink(actions, item, storyName, hasStory, dark);
@@ -186,6 +202,7 @@ export const StoryActions = (props: Props): Node => {
186202
DELETE,
187203
REPORT,
188204
VISIBLE,
205+
NOT_VISIBLE,
189206
VIEWERS,
190207
SHARE_LINK_INFO,
191208
].map((item: string) => (actions[item]

0 commit comments

Comments
 (0)