Skip to content

Commit

Permalink
Extend make visible in stats (#2168)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
uzorjchibuzor authored Oct 20, 2022
1 parent 7b9bf25 commit a3e05a8
Show file tree
Hide file tree
Showing 35 changed files with 220 additions and 200 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ GEM
ruby2_keywords (~> 0.0.1)
netrc (0.11.0)
nio4r (2.5.8)
nokogiri (1.13.8)
nokogiri (1.13.9)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
oauth2 (1.4.9)
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/moments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ def moment_params
end

def set_association_variables!
@categories = current_user.categories.order(created_at: :desc)
@categories = current_user.categories.is_visible
.or(Category.where(id: @moment.category_ids))
.order(created_at: :desc)
@category = Category.new
@moods = current_user.moods.order(created_at: :desc)
@moods = current_user.moods.is_visible.or(Mood.where(id: @moment.mood_ids))
.order(created_at: :desc)
@mood = Mood.new
@strategies = associated_strategies
@strategy = Strategy.new
Expand All @@ -127,6 +130,8 @@ def associated_strategies
Strategy.where(user: @viewers).each do |strategy|
strategy_ids << strategy.id if strategy.viewer?(current_user)
end
Strategy.where(id: strategy_ids).order(created_at: :desc)
Strategy.is_visible.where(id: strategy_ids)
.or(Strategy.where(id: @moment.strategy_ids))
.order(created_at: :desc)
end
end
6 changes: 4 additions & 2 deletions app/controllers/strategies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def quick_create
def new
@viewers = current_user.allies_by_status(:accepted)
@strategy = Strategy.new
@categories = current_user.categories.order('created_at DESC')
@categories = current_user.categories.is_visible.order('created_at DESC')
@category = Category.new
@strategy.build_perform_strategy_reminder
end
Expand All @@ -52,7 +52,9 @@ def edit
redirect_to_path(strategy_path(@strategy))
end
@viewers = current_user.allies_by_status(:accepted)
@categories = current_user.categories.order('created_at DESC')
@categories = current_user.categories.is_visible
.or(Category.where(id: @strategy.category_ids))
.order('created_at DESC')
@category = Category.new
PerformStrategyReminder.find_or_initialize_by(strategy_id: @strategy.id)
end
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/categories_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def actions_setup(element, url_helper)
link: link
},
delete: action_delete(url_helper),
visible: get_visible(element.visible)
not_visible: !element.visible && get_visible(element.visible),
visible: element.visible && get_visible(element.visible)
}
end

Expand Down
17 changes: 12 additions & 5 deletions app/helpers/moments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def present_moment_or_strategy(element)
{ name: element.name,
link: present_object[:url_helper],
date: TimeAgo.created_or_edited(element),
actions: moment_or_strategy_actions(element, present_object),
actions: element_actions(element, present_object),
draft: !element.published? ? t('draft') : nil,
categories: element.category_names_and_slugs,
moods: present_object[:moods],
Expand All @@ -86,13 +86,20 @@ def get_resources_data(moment, current_user)

private

def moment_or_strategy_actions(element, present_object)
def element_actions(element, present_object)
actions = user_actions(element,
present_object,
element.user_id == current_user&.id)
{ edit: actions[:edit], delete: actions[:delete],
viewers: actions[:viewers], visible: actions[:visible],
share_link_info: actions[:share_link_info] }
{
edit: actions[:edit],
delete: actions[:delete],
viewers: actions[:viewers],
not_visible: (!element.is_a?(Moment) &&
!element.visible) &&
actions[:visible],
visible: (element.is_a?(Moment) || element.visible) && actions[:visible],
share_link_info: actions[:share_link_info]
}.delete_if { |_, value| value == false }
end

def story_by(element)
Expand Down
23 changes: 23 additions & 0 deletions app/helpers/viewers_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ def get_viewers_for(data, data_type)
result.uniq
end

def element_visibility_based_props(element)
{
dark: true,
actions: {
delete: {
name: t('common.actions.delete'),
link: url_for(element),
dataConfirm: t('common.actions.confirm'),
dataMethod: 'delete'
},
edit: {
name: t('common.actions.edit'),
link: url_for([:edit, element])
},
not_visible: !element.visible && get_visible(element.visible),
viewers: element.respond_to?(:viewers) &&
get_viewer_list(element.viewers, nil),
visible: element.visible && get_visible(element.visible)
}.delete_if { |_, value| value == false },
storyName: element.name
}
end

private

def get_link(link)
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/visible_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
module VisibleHelper
def get_visible(visible)
return t('shared.stats.visible_in_stats') if visible

t('shared.stats.not_visible_in_stats')
end
end
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#

class Category < ApplicationRecord
include IsVisibleConcern
extend FriendlyId

friendly_id :name
Expand Down
14 changes: 14 additions & 0 deletions app/models/concerns/common_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ def category_names_and_slugs
names_and_slugs_hash(categories.pluck(:name, :slug), 'categories')
end

def elements_array_data(elements)
elements.each do |element|
associated_elements = element.pluralize
klass = element.capitalize.constantize
if send(element).is_a?(Array)
element_ids = send(element).collect(&:to_i)
send("#{associated_elements}=",
klass.where(user_id: user_id, id: element_ids))
else
send("#{associated_elements}=", klass.none)
end
end
end

private

def names_and_slugs_hash(data, model_name)
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/is_visible_concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true
module IsVisibleConcern
extend ActiveSupport::Concern

included do
scope :is_visible, -> { where(visible: true) }
end
end
27 changes: 3 additions & 24 deletions app/models/moment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class Moment < ApplicationRecord
friendly_id :name
serialize :viewers, Array

before_save :category_array_data
before_save do
elements_array_data(%w[category mood strategy])
end
before_save :viewers_array_data
before_save :mood_array_data
before_save :strategy_array_data

belongs_to :user

Expand Down Expand Up @@ -80,31 +80,10 @@ def self.find_secret_share!(identifier)
)
end

def category_array_data
return unless category.is_a?(Array)

category_ids = category.collect(&:to_i)
self.categories = Category.where(user_id: user_id, id: category_ids)
end

def viewers_array_data
self.viewers = viewers.collect(&:to_i) if viewers.is_a?(Array)
end

def mood_array_data
return unless mood.is_a?(Array)

mood_ids = mood.collect(&:to_i)
self.moods = Mood.where(user_id: user_id, id: mood_ids)
end

def strategy_array_data
return unless strategy.is_a?(Array)

strategy_ids = strategy.collect(&:to_i)
self.strategies = Strategy.where(user_id: user_id, id: strategy_ids)
end

def owned_by?(user)
user&.id == user_id
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/mood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#

class Mood < ApplicationRecord
include IsVisibleConcern

USER_DATA_ATTRIBUTES = %w[
id
name
Expand Down
13 changes: 5 additions & 8 deletions app/models/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#

class Strategy < ApplicationRecord
include IsVisibleConcern
include Viewer
include CommonMethods
extend FriendlyId
Expand All @@ -39,7 +40,10 @@ class Strategy < ApplicationRecord
friendly_id :name
serialize :viewers, Array

before_save :category_array_data
before_save do
elements_array_data(['category'])
end

before_save :viewers_array_data

belongs_to :user
Expand Down Expand Up @@ -70,13 +74,6 @@ def viewers_array_data
self.viewers = viewers.collect(&:to_i) if viewers.is_a?(Array)
end

def category_array_data
return unless category.is_a?(Array)

category_ids = category.collect(&:to_i)
self.categories = Category.where(user_id: user_id, id: category_ids)
end

def published?
published_at.present?
end
Expand Down
18 changes: 1 addition & 17 deletions app/views/categories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,7 @@
<div class="gridItemBoxDark gridRowSpaceBetween smallMarginTop">
<div class="gridRowSpaceBetween">
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
<%= react_component('StoryActions', props: {
dark: true,
actions: {
delete: {
name: t('common.actions.delete'),
link: url_for(@category),
dataConfirm: t('common.actions.confirm'),
dataMethod: 'delete'
},
edit: {
name: t('common.actions.edit'),
link: edit_category_path(@category)
},
visible: get_visible(@category.visible)
},
storyName: @category.name
}) %>
<%= react_component('StoryActions', props: element_visibility_based_props(@category))%>
</div>
</div>
<%= render partial: '/tag_usage/index' %>
18 changes: 1 addition & 17 deletions app/views/moods/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,7 @@
<div class="gridItemBoxDark gridRowSpaceBetween smallMarginTop">
<div class="gridRowSpaceBetween">
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
<%= react_component('StoryActions', props: {
dark: true,
actions: {
delete: {
name: t('common.actions.delete'),
link: url_for(@mood),
dataConfirm: t('common.actions.confirm'),
dataMethod: 'delete'
},
edit: {
name: t('common.actions.edit'),
link: edit_mood_path(@mood)
},
visible: get_visible(@mood.visible)
},
storyName: @mood.name
}) %>
<%= react_component('StoryActions', props: element_visibility_based_props(@mood)) %>
</div>
</div>
<%= render partial: '/tag_usage/index' %>
19 changes: 1 addition & 18 deletions app/views/strategies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@
<div class="gridRowSpaceBetween">
<div class="gridRowSpaceBetween">
<div class="smallMarginRight tinyTitle"><%= t('common.actions.plural') %></div>
<%= react_component('StoryActions', props: {
dark: true,
actions: {
edit: {
link: edit_strategy_path(@strategy),
name: t('common.actions.edit')
},
delete: {
name: t('common.actions.delete'),
link: url_for(@strategy),
dataMethod: 'delete',
dataConfirm: t('common.actions.confirm')
},
viewers: get_viewer_list(@strategy.viewers, nil),
visible: get_visible(@strategy.visible)
},
storyName: @strategy.name
}) %>
<%= react_component('StoryActions', props: element_visibility_based_props(@strategy)) %>
</div>
<%= print_reminders(@strategy) %>
</div>
Expand Down
19 changes: 18 additions & 1 deletion client/app/components/Story/StoryActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
faDoorOpen,
faDoorClosed,
faExclamationTriangle,
faEyeSlash,
faCalendarPlus,
faCalendarMinus,
faChartLine,
Expand Down Expand Up @@ -37,6 +38,7 @@ export type Actions = {
remove_from_google_cal?: Action,
viewers?: string,
visible?: string,
not_visible?: string,
};

export type Props = {
Expand All @@ -53,6 +55,7 @@ const LEAVE = 'leave';
const REPORT = 'report';
const VIEWERS = 'viewers';
const VISIBLE = 'visible';
const NOT_VISIBLE = 'not_visible';
const ADD_TO_G_CAL = 'add_to_google_cal';
const REMOVE_FROM_G_CAL = 'remove_from_google_cal';
const SHARE_LINK_INFO = 'share_link_info';
Expand Down Expand Up @@ -83,6 +86,14 @@ const classMap = (dark: ?boolean) => {
aria-label={I18n.t('shared.stats.visible_in_stats')}
/>
),
not_visible: (
<FontAwesomeIcon
icon={faEyeSlash}
className={className}
tabIndex={0}
aria-label={I18n.t('shared.stats.not_visible_in_stats')}
/>
),
add_to_google_cal: (
<FontAwesomeIcon icon={faCalendarPlus} className={className} />
),
Expand Down Expand Up @@ -165,7 +176,12 @@ const displayItem = (
hasStory: ?boolean,
dark: ?boolean,
) => {
if (item === VIEWERS || item === VISIBLE || item === SHARE_LINK_INFO) {
if (
item === VIEWERS
|| item === VISIBLE
|| item === NOT_VISIBLE
|| item === SHARE_LINK_INFO
) {
return displayNonLink(actions, item, hasStory, dark);
}
return displayLink(actions, item, storyName, hasStory, dark);
Expand All @@ -186,6 +202,7 @@ export const StoryActions = (props: Props): Node => {
DELETE,
REPORT,
VISIBLE,
NOT_VISIBLE,
VIEWERS,
SHARE_LINK_INFO,
].map((item: string) => (actions[item]
Expand Down
Loading

0 comments on commit a3e05a8

Please sign in to comment.