Fix global activities jsonb field [SCI-3830] (#2030)

* Fix global activities jsonb field

* Fix tests
This commit is contained in:
aignatov-bio 2019-09-05 08:33:53 +02:00 committed by aignatov-bio
parent 759b3acfe9
commit b1f9e7f1ca
14 changed files with 1146 additions and 29 deletions

View file

@ -7,12 +7,12 @@ module GlobalActivitiesHelper
def generate_activity_content(activity, no_links = false) def generate_activity_content(activity, no_links = false)
parameters = {} parameters = {}
activity.values[:message_items].each do |key, value| activity.message_items.each do |key, value|
parameters[key] = parameters[key] =
if value.is_a? String if value.is_a? String
value value
elsif value[:type] == 'Time' # use saved date for printing elsif value['type'] == 'Time' # use saved date for printing
l(Time.at(value[:value]), format: :full_date) l(Time.at(value['value']), format: :full_date)
else else
no_links ? generate_name(value) : generate_link(value, activity) no_links ? generate_name(value) : generate_link(value, activity)
end end
@ -21,15 +21,15 @@ module GlobalActivitiesHelper
I18n.t("global_activities.content.#{activity.type_of}_html", parameters.symbolize_keys), I18n.t("global_activities.content.#{activity.type_of}_html", parameters.symbolize_keys),
team: activity.team team: activity.team
) )
rescue StandardError => ex rescue StandardError => e
Rails.logger.error(ex.message) Rails.logger.error(e.message)
Rails.logger.error(ex.backtrace.join("\n")) Rails.logger.error(e.backtrace.join("\n"))
I18n.t('global_activities.index.content_generation_error', activity_id: activity.id) I18n.t('global_activities.index.content_generation_error', activity_id: activity.id)
end end
def generate_link(message_item, activity) def generate_link(message_item, activity)
obj = message_item[:type].constantize.find_by_id(message_item[:id]) obj = message_item['type'].constantize.find_by_id(message_item['id'])
return message_item[:value] unless obj return message_item['value'] unless obj
current_value = generate_name(message_item) current_value = generate_name(message_item)
team = activity.team team = activity.team
@ -98,10 +98,10 @@ module GlobalActivitiesHelper
end end
def generate_name(message_item) def generate_name(message_item)
obj = message_item[:type].constantize.find_by_id(message_item[:id]) obj = message_item['type'].constantize.find_by_id(message_item['id'])
return message_item[:value] unless obj return message_item['value'] unless obj
value = obj.public_send(message_item[:value_for] || 'name') value = obj.public_send(message_item['value_for'] || 'name')
value = I18n.t('global_activities.index.no_name') if value.blank? value = I18n.t('global_activities.index.no_name') if value.blank?
value value

View file

@ -8,7 +8,6 @@ module ActivityValuesModel
# rubocop:enable Style/ClassVars # rubocop:enable Style/ClassVars
included do included do
serialize :values, JsonbHashSerializer
after_initialize :init_default_values, if: :new_record? after_initialize :init_default_values, if: :new_record?
before_create :add_user before_create :add_user
end end

View file

@ -9,7 +9,7 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<% name = subject&.name || breadcrumbs[:experiment] %> <% name = subject&.name || breadcrumbs['experiment'] %>
<span title="<%= name %>"> <span title="<%= name %>">
<%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>

View file

@ -17,7 +17,7 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<% name = subject&.name || breadcrumbs[:my_module] %> <% name = subject&.name || breadcrumbs['my_module'] %>
<span title="<%= name %>"> <span title="<%= name %>">
<%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>

View file

@ -9,8 +9,8 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<span title="<%= breadcrumbs[:project] %>"> <span title="<%= breadcrumbs['project'] %>">
<%= breadcrumbs[:project]&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= breadcrumbs['project']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>
<% end %> <% end %>
</div> </div>

View file

@ -21,8 +21,8 @@
<% else %> <% else %>
<div class="ga-breadcrumb"> <div class="ga-breadcrumb">
<span class="fas fa-edit"></span> <span class="fas fa-edit"></span>
<span title="<%= breadcrumbs[:protocol] %>"> <span title="<%= breadcrumbs['protocol'] %>">
<%= breadcrumbs[:protocol]&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= breadcrumbs['protocol']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>
</div> </div>
<% end %> <% end %>

View file

@ -8,8 +8,8 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<span title="<%= breadcrumbs[:report] %>"> <span title="<%= breadcrumbs['report'] %>">
<%= breadcrumbs[:report]&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= breadcrumbs['report']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>
<% end %> <% end %>
</div> </div>

View file

@ -8,8 +8,8 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<span title="<%= breadcrumbs[:repository] %>"> <span title="<%= breadcrumbs['repository'] %>">
<%= breadcrumbs[:repository]&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= breadcrumbs['repository']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>
<% end %> <% end %>
</div> </div>

View file

@ -10,7 +10,7 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name) %> title: subject.name) %>
<% else %> <% else %>
<% name = subject&.name || breadcrumbs[:result] %> <% name = subject&.name || breadcrumbs['result'] %>
<span title="<%= name %>"> <span title="<%= name %>">
<%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= name&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>

View file

@ -6,8 +6,8 @@
subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH), subject.name&.truncate(Constants::NAME_TRUNCATION_LENGTH),
title: subject.name %> title: subject.name %>
<% else %> <% else %>
<span title="<%= breadcrumbs[:team] %>"> <span title="<%= breadcrumbs['team'] %>">
<%= breadcrumbs[:team]&.truncate(Constants::NAME_TRUNCATION_LENGTH) %> <%= breadcrumbs['team']&.truncate(Constants::NAME_TRUNCATION_LENGTH) %>
</span> </span>
<% end %> <% end %>
</div> </div>

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class ConvertActivitiesJsonToJsonb < ActiveRecord::Migration[5.2]
def change
change_column :activities, :values, 'jsonb USING CAST(values AS jsonb)'
ActiveRecord::Base.connection.execute("
UPDATE activities SET values = REGEXP_REPLACE(values::text, '^\"||\"$||\\\\', '', 'g')::jsonb
")
end
end

1108
db/schema.rb Normal file

File diff suppressed because it is too large Load diff

View file

@ -65,7 +65,7 @@ describe Activity, type: :model do
it 'adds user to message items' do it 'adds user to message items' do
activity.save activity.save
expect(activity.message_items).to include(user: be_an(Hash)) expect(activity.message_items).to include('user' => be_an(Hash))
end end
end end
@ -73,14 +73,14 @@ describe Activity, type: :model do
context 'when do not have subject' do context 'when do not have subject' do
it 'does not add breadcrumbs to activity' do it 'does not add breadcrumbs to activity' do
expect { old_activity.generate_breadcrumbs } expect { old_activity.generate_breadcrumbs }
.not_to(change { activity.values[:breadcrumbs] }) .not_to(change { activity.values['breadcrumbs'] })
end end
end end
context 'when have subject' do context 'when have subject' do
it 'adds breadcrumbs to activity' do it 'adds breadcrumbs to activity' do
expect { activity.generate_breadcrumbs } expect { activity.generate_breadcrumbs }
.to(change { activity.values[:breadcrumbs] }) .to(change { activity.values['breadcrumbs'] })
end end
context 'when subject is a my_module' do context 'when subject is a my_module' do

View file

@ -71,7 +71,7 @@ describe Activities::CreateActivityService do
a = service_call.activity a = service_call.activity
expect(a.message_items[:project].keys) expect(a.message_items[:project].keys)
.to contain_exactly('type', 'value', 'id', 'value_for') .to contain_exactly(:type, :value, :id, :value_for)
end end
it 'adds object project to message items as hash' do it 'adds object project to message items as hash' do