Permalink
Browse files

made everything that was being stored in json format in the database …

…serializable
  • Loading branch information...
HoneyryderChuck committed Mar 10, 2012
1 parent b8c38a8 commit b5c11d09789a7f196bf0026e2de493e19afdb9f7
Showing with 228 additions and 121 deletions.
  1. +2 −2 app/controllers/application_controller.rb
  2. +2 −2 app/controllers/statements/statement_images_controller.rb
  3. +3 −4 app/controllers/users/activations_controller.rb
  4. +10 −10 app/controllers/users/social_module.rb
  5. +9 −9 app/controllers/users/user_sessions_controller.rb
  6. +1 −1 app/controllers/users/users_controller.rb
  7. +5 −5 app/helpers/mail_helper.rb
  8. +1 −0 app/models/pending_action.rb
  9. +2 −1 app/models/shortcut_command.rb
  10. +2 −2 app/models/shortcut_url.rb
  11. +1 −1 app/models/statements/statement.rb
  12. +6 −6 app/views/activity_tracking_mailer/_operation_template.html.erb
  13. +1 −1 app/views/activity_tracking_mailer/activity_templates/_root_statements_activity_template.html.erb
  14. +1 −1 app/views/activity_tracking_mailer/activity_templates/_statement_activity_template.html.erb
  15. +105 −0 db/migrate/20120225183159_change_string_to_text_attributes_on_models_that_serialize.rb
  16. +6 −6 db/schema.rb
  17. +4 −5 features/step_definitions/discuss_steps.rb
  18. +12 −12 lib/activity_tracking_service/activity_tracking_service.rb
  19. +2 −0 lib/activity_tracking_service/event.rb
  20. +1 −1 lib/social_service/acts_as_social.rb
  21. +1 −1 lib/social_service/social_identifier.rb
  22. +2 −2 lib/social_service/social_service.rb
  23. +4 −4 test/fixtures/events.yml
  24. +2 −2 test/fixtures/social_identifiers.yml
  25. +4 −4 test/functional/activations_controller_test.rb
  26. +5 −5 test/functional/users_controller_test.rb
  27. +23 −23 test/unit/activity_tracking_mailer_test.rb
  28. +8 −8 test/unit/shortcut_url_test.rb
  29. +1 −1 test/unit/statement_node_test.rb
  30. +2 −2 test/unit/user_test.rb
@@ -537,8 +537,8 @@ def log_message_error(e, message)
def shortcut
begin
shortcut = ShortcutUrl.find(params[:shortcut])
- command = JSON.parse(shortcut.command)
- url = send("#{command['operation']}_path", command['params'].merge({:locale => command['language']}))
+ command = shortcut.command
+ url = send("#{command[:operation]}_path", command[:params].merge({:locale => command[:language]}))
redirect_to url
rescue ActiveRecord::RecordNotFound
redirect_to discuss_search_url(:search_terms => params[:shortcut])
@@ -32,7 +32,7 @@ def new
#
def create
image = StatementImage.create(params[:statement_image])
- PendingAction.find(params[:pending_action_id]).update_attributes(:action => {:image_id => image.id}.to_json)
+ PendingAction.find(params[:pending_action_id]).update_attributes(:action => {:image_id => image.id})
respond_to(:html,:js)
end
@@ -75,7 +75,7 @@ def update
#
def load
@pending = PendingAction.find(params[:id])
- @statement_image = StatementImage.find(JSON.parse(@pending.action)['image_id'])
+ @statement_image = StatementImage.find(@pending.action[:image_id])
respond_to do |format|
if @statement_image.image.exists?
set_info 'discuss.messages.image_uploaded', :type => I18n.t("discuss.statements.types.#{params[:type]}")
@@ -39,7 +39,7 @@ def activate
redirect_or_render_with_error(base_url, "users.activation.messages.no_account")
return
elsif via_form # Coming from setup basic profile form
- if params[:user].delete(:agreement).nil?
+ if params[:user].delete(:agreement).blank?
redirect_or_render_with_error(base_url, "users.activation.messages.no_agreement")
return
elsif params[:user][:full_name].try(:strip).blank?
@@ -51,8 +51,7 @@ def activate
redirect_or_render_with_error(base_url, "users.activation.messages.already_active")
return
end
-
-
+
if @user.email or @user.has_verified_email? params[:user][:email]
# Given email is a verified email, therefore, no activation is needed
if @user.activate!(params[:user]) and @user.profile.save # so that the name persists
@@ -106,7 +105,7 @@ def activate_email
else
@user = @action.pending
User.transaction do
- if @user.update_attributes(JSON.parse(@action.action))
+ if @user.update_attributes(@action.action)
@action.update_attribute(:status, true)
UserSession.create(@user, false)
@user.deliver_activation_confirmation!
@@ -12,13 +12,13 @@ def create_social
@user = User.new
opts ={}
- opts = {:social_identifiers => [SocialIdentifier.new(:identifier => profile_info['identifier'],
- :provider_name => profile_info['providerName'],
- :profile_info => profile_info.to_json )] }
+ opts = {:social_identifiers => [SocialIdentifier.new(:identifier => profile_info[:identifier],
+ :provider_name => profile_info[:providerName],
+ :profile_info => profile_info )] }
User.transaction do
if @user.signup!(opts)
- SocialService.instance.map(profile_info['identifier'], @user.id)
+ SocialService.instance.map(profile_info[:identifier], @user.id)
later_call_with_info(redirect_url, setup_basic_profile_url(@user.perishable_token))
else
later_call_with_error(redirect_url, signup_url, @user.social_identifiers.first)
@@ -47,7 +47,7 @@ def create_social
#
def setup_basic_profile
@user = User.find_by_perishable_token(params[:activation_code], 1.week)
- @profile_info = JSON.parse(@user.social_identifiers.first.profile_info)
+ @profile_info = @user.social_identifiers.first.profile_info
if @user.nil?
redirect_or_render_with_error(base_url, "users.activation.messages.no_account")
elsif @user.active?
@@ -73,12 +73,12 @@ def add_social
if params[:token]
User.transaction do
profile_info = SocialService.instance.get_profile_info(params[:token])
- social_id = current_user.add_social_identifier(profile_info['identifier'],
- profile_info['providerName'],
- profile_info.to_json )
- account_name = I18n.t("users.social_accounts.providers.#{profile_info['providerName'].downcase}")
+ social_id = current_user.add_social_identifier(profile_info[:identifier],
+ profile_info[:providerName],
+ profile_info )
+ account_name = I18n.t("users.social_accounts.providers.#{profile_info[:providerName].downcase}")
if social_id.save
- SocialService.instance.map(profile_info['identifier'], current_user.id)
+ SocialService.instance.map(profile_info[:identifier], current_user.id)
if later_call_url
later_call_with_info(redirect_url,
later_call_url,
@@ -40,16 +40,16 @@ def create_social
profile_info = SocialService.instance.get_profile_info(params[:token])
user = nil
User.transaction do
- if profile_info['primaryKey'].nil? or (user = User.find(profile_info['primaryKey'])).nil?
- session[:identifier] = profile_info.to_json
- invalid_social = SocialIdentifier.find_by_identifier(profile_info['identifier'])
+ if profile_info[:primaryKey].nil? or (user = User.find(profile_info[:primaryKey])).nil?
+ session[:identifier] = profile_info
+ invalid_social = SocialIdentifier.find_by_identifier(profile_info[:identifier])
invalid_social.destroy if invalid_social
later_call_with_error(redirect_url, signin_path, 'users.signin.messages.failed_social')
else
- if social = user.identified_by?(profile_info['identifier'])
- social.update_attribute(:profile_info, profile_info.to_json)
+ if social = user.identified_by?(profile_info[:identifier])
+ social.update_attribute(:profile_info, profile_info)
else
- user.add_social_identifier( profile_info['identifier'], profile_info['providerName'], profile_info.to_json )
+ user.add_social_identifier( profile_info[:identifier], profile_info[:providerName], profile_info )
end
if user.active? # user was already actived, i.e. he has an email account defined
@user_session = UserSession.new(user)
@@ -88,9 +88,9 @@ def destroy
protected
def add_social_to_user(user)
- profile_info = JSON.parse(session[:identifier])
- social_identifier = SocialIdentifier.create(:identifier => profile_info['identifier'],
- :provider_name => profile_info['providerName'],
+ profile_info = session[:identifier]
+ social_identifier = SocialIdentifier.create(:identifier => profile_info[:identifier],
+ :provider_name => profile_info[:providerName],
:profile_info => session[:identifier],
:user => user)
session.delete(:identifier)
@@ -118,7 +118,7 @@ def update_email
else
# Validate per Mail and change in a pending action
- pending_action = PendingAction.create(:action => params[:user].to_json, :pending => current_user)
+ pending_action = PendingAction.create(:action => params[:user], :pending => current_user)
current_user.deliver_activate_email!(params[:user][:email], pending_action.uuid)
redirect_or_render_with_info(settings_path, "users.users.messages.activate_email") do |page|
close_panel(page)
View
@@ -24,19 +24,19 @@ def get_document_in_preferred_language(documents, language_ids)
end
def inline_statement_link(document, event)
- if event['info_type']
- image_name = 'bi_' + event['info_type']
+ if event[:info_type]
+ image_name = 'bi_' + event[:info_type]
else
- image_name = event['type']
+ image_name = event[:type]
end
icon_url = full_url("/images/page/discuss/statement_icons/#{image_name}.png")
content_tag(:li,
- :class => "statement_link #{event['type']}_link",
+ :class => "statement_link #{event[:type]}_link",
:style => "list-style: none; margin: 7px 0;") do
concat(
image_tag(icon_url, :alt => '') +
link_to(document[1],
- statement_node_url(event['id'], :locale => Language[document[0].to_i].code),
+ statement_node_url(event[:id], :locale => Language[document[0].to_i].code),
:style => "color: #21587f; text-decoration: none; font-weight: bold; padding-left: 5px; vertical-align: top;")
)
end
@@ -1,6 +1,7 @@
class PendingAction < ActiveRecord::Base
include UUIDHelper
belongs_to :pending, :polymorphic => true
+ serialize :action, Hash
set_primary_key 'uuid'
@@ -1,5 +1,6 @@
class ShortcutCommand < ActiveRecord::Base
has_many :shortcut_urls, :dependent => :destroy
+ serialize :command, Hash
validates_uniqueness_of :command
@@ -8,7 +9,7 @@ class << self
def build_command(opts)
{:operation => opts[:operation],
:params => opts[:params],
- :language => opts[:language]}.to_json
+ :language => opts[:language]}
end
end
@@ -2,7 +2,7 @@ class ShortcutUrl < ActiveRecord::Base
belongs_to :shortcut_command
set_primary_key 'shortcut'
- delegate :command, :to => :shortcut_command
+ delegate :command, :to => :shortcut_command, :allow_nil => true
validates_length_of :shortcut, :maximum => 101
@@ -33,7 +33,7 @@ def find_or_create(opts={})
opts[:shortcut] = shortcut
s = self.new
- s.shortcut_command = ShortcutCommand.find_by_command(shortcut_command[:command]) || ShortcutCommand.new(shortcut_command)
+ s.shortcut_command = ShortcutCommand.find_by_command(shortcut_command[:command].to_yaml) || ShortcutCommand.new(shortcut_command)
opts.each {|k,v| s.send("#{k}=", v)}
s.save ? s : nil
end
@@ -70,7 +70,7 @@ def has_author? user
def statement_image_id=(image_id)
return if image_id.blank?
@pending_image = PendingAction.find(image_id)
- self.statement_image = StatementImage.find(JSON.parse(@pending_image.action)['image_id'])
+ self.statement_image = StatementImage.find(@pending_image.action[:image_id])
end
@@ -1,20 +1,20 @@
<% if !events.empty? %>
<% events.each_with_index do |event, index| %>
<% if index == 0 %>
- <% parent_document = get_document_in_preferred_language(event["parent_documents"], @preferred_language_ids) %>
+ <% parent_document = get_document_in_preferred_language(event[:parent_documents], @preferred_language_ids) %>
<%= content_tag :p, I18n.t("mailers.activity_tracking.statements.#{operation}_#{type.pluralize}",
:title => parent_document[1],
:locale => @language.code) if render_level == 0 %>
<ul style="padding-left: 20px;">
<% end %>
- <% children_level = @events[event['level']+1] %>
- <% @events[event['level']][event['parent_id']][event['type']][event['operation']] -= [event] %>
+ <% children_level = @events[event[:level]+1] %>
+ <% @events[event[:level]][event[:parent_id]][event[:type]][event[:operation]] -= [event] %>
<%= render :partial => "activity_tracking_mailer/activity_templates/statement_activity_template",
:locals => {:event => event} %>
<%= render :partial => "activity_tracking_mailer/children_template",
- :locals => {:parent_id => event['id'],
- :events => children_level[event['id']],
- :render_level => render_level + 1} if children_level and children_level[event['id']] %>
+ :locals => {:parent_id => event[:id],
+ :events => children_level[event[:id]],
+ :render_level => render_level + 1} if children_level and children_level[event[:id]] %>
<% end %>
</ul>
<% end %>
@@ -16,7 +16,7 @@
<ul style="padding-left: 20px;">
<% @root_events.each do |event| %>
- <% document = get_document_in_preferred_language(event["documents"], @preferred_language_ids) %>
+ <% document = get_document_in_preferred_language(event[:documents], @preferred_language_ids) %>
<%= inline_statement_link(document, event) %>
<% end %>
</ul>
@@ -1,4 +1,4 @@
-<% document = get_document_in_preferred_language(event["documents"], @preferred_language_ids) %>
+<% document = get_document_in_preferred_language(event[:documents], @preferred_language_ids) %>
<%= inline_statement_link(document, event) %>
@@ -0,0 +1,105 @@
+class ChangeStringToTextAttributesOnModelsThatSerialize < ActiveRecord::Migration
+ def self.up
+ # Shortcut Command
+ rename_column :shortcut_commands, :command, :old_command
+ add_column :shortcut_commands, :command, :text
+
+ ShortcutCommand.find_each do |sh|
+ command = JSON.parse(sh.old_command)
+ command.symbolize_keys!
+ sh.command = command
+ sh.save
+ print '.'
+ end
+ remove_column :shortcut_commands, :old_command
+
+ # Shortcut Command
+ rename_column :pending_actions, :action, :old_action
+ add_column :pending_actions, :action, :text
+
+ PendingAction.find_each do |pa|
+ action = JSON.parse(pa.old_action)
+ action.symbolize_keys!
+ pa.action = action
+ pa.save
+ print '.'
+ end
+ remove_column :pending_actions, :old_action
+
+ # Event
+ rename_column :events, :event, :old_event
+ add_column :events, :event, :text
+
+ Event.find_each do |e|
+ event = JSON.parse(e.old_event)
+ event.symbolize_keys!
+ e.event = event
+ e.save
+ print '.'
+ end
+ remove_column :events, :old_event
+
+ # Social Identifier
+ rename_column :social_identifiers, :profile_info, :old_profile_info
+ add_column :social_identifiers, :profile_info, :text
+
+ SocialIdentifier.find_each do |si|
+ profile_info = JSON.parse(si.old_profile_info)
+ profile_info.symbolize_keys!
+ si.profile_info = profile_info
+ si.save
+ print '.'
+ end
+ remove_column :social_identifiers, :old_profile_info
+ end
+
+ def self.down
+ # Shortcut Command
+ rename_column :shortcut_commands, :command, :old_command
+ add_column :shortcut_commands, :command, :text
+
+ ShortcutCommand.find_each do |sh|
+ command = sh.old_command
+ sh.command = command.to_json
+ sh.save
+ print '.'
+ end
+ remove_column :shortcut_commands, :old_command
+
+ # Shortcut Command
+ rename_column :pending_actions, :action, :old_action
+ add_column :pending_actions, :action, :text
+
+ PendingAction.find_each do |pa|
+ action = pa.old_action
+ pa.action = action.to_json
+ pa.save
+ print '.'
+ end
+ remove_column :pending_actions, :old_action
+
+ # Event
+ rename_column :events, :event, :old_event
+ add_column :events, :event, :text
+
+ Event.find_each do |e|
+ event = e.old_event
+ e.event = event.to_json
+ e.save
+ print '.'
+ end
+ remove_column :events, :old_event
+
+ # Social Identifier
+ rename_column :social_identifiers, :profile_info, :old_profile_info
+ add_column :social_identifiers, :profile_info, :text
+
+ SocialIdentifier.find_each do |si|
+ profile_info = si.old_profile_info
+ si.profile_info = profile_info.to_json
+ si.save
+ print '.'
+ end
+ remove_column :social_identifiers, :old_profile_info
+ end
+end
Oops, something went wrong.

0 comments on commit b5c11d0

Please sign in to comment.