From db1e4abf17405f1cbfdce30e89498e542f5c0e86 Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Tue, 10 Mar 2015 11:36:28 -0300 Subject: [PATCH] favorite_enterprise: track actions to wall --- app/helpers/action_tracker_helper.rb | 6 ++++++ app/models/enterprise.rb | 2 +- app/models/favorite_enterprise_person.rb | 17 ++++++++++++++++- app/models/person.rb | 5 +++-- app/models/profile.rb | 6 +++++- app/views/profile/_favorite_enterprise.html.erb | 15 +++++++++++++++ config/initializers/action_tracker.rb | 3 +++ db/migrate/20150310132902_add_id_to_favorite_enterprises_people.rb | 26 ++++++++++++++++++++++++++ db/schema.rb | 12 +++++++++--- 9 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 app/views/profile/_favorite_enterprise.html.erb create mode 100644 db/migrate/20150310132902_add_id_to_favorite_enterprises_people.rb diff --git a/app/helpers/action_tracker_helper.rb b/app/helpers/action_tracker_helper.rb index 196d9ad..e6f8a66 100644 --- a/app/helpers/action_tracker_helper.rb +++ b/app/helpers/action_tracker_helper.rb @@ -85,4 +85,10 @@ module ActionTrackerHelper } end + def favorite_enterprise_description ta + _('favorited enterprise %{title}') % { + title: link_to(truncate(ta.get_enterprise_name), ta.get_enterprise_url), + } + end + end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 5cc43f6..7024c57 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -23,7 +23,7 @@ class Enterprise < Organization has_many :production_costs, :as => :owner has_many :favorite_enterprise_people - has_many :fans, through: :favorite_enterprise_people, source: :person + has_many :fans, source: :person, through: :favorite_enterprise_people def product_categories ProductCategory.by_enterprise(self) diff --git a/app/models/favorite_enterprise_person.rb b/app/models/favorite_enterprise_person.rb index f46cbe3..764dc2a 100644 --- a/app/models/favorite_enterprise_person.rb +++ b/app/models/favorite_enterprise_person.rb @@ -1,8 +1,23 @@ class FavoriteEnterprisePerson < ActiveRecord::Base - self.table_name = :favorite_enteprises_people + attr_accessible :person, :enterprise + + track_actions :favorite_enterprise, :after_create, keep_params: [:enterprise_name, :enterprise_url], if: proc{ |f| f.is_trackable? } belongs_to :enterprise belongs_to :person + protected + + def is_trackable? + self.enterprise.public? + end + + def enterprise_name + self.enterprise.short_name(nil) + end + def enterprise_url + self.enterprise.url + end + end diff --git a/app/models/person.rb b/app/models/person.rb index 63b79e2..c1e8378 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -81,6 +81,9 @@ roles] } has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy + has_many :favorite_enterprise_people + has_many :favorite_enterprises, source: :enterprise, through: :favorite_enterprise_people + has_and_belongs_to_many :acepted_forums, :class_name => 'Forum', :join_table => 'terms_forum_people' has_and_belongs_to_many :articles_with_access, :class_name => 'Article', :join_table => 'article_privacy_exceptions' @@ -315,8 +318,6 @@ roles] } ] end - has_and_belongs_to_many :favorite_enterprises, :class_name => 'Enterprise', :join_table => 'favorite_enteprises_people' - def email_domain user && user.email_domain || environment.default_hostname(true) end diff --git a/app/models/profile.rb b/app/models/profile.rb index 9946e84..ca0ddf5 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -744,7 +744,11 @@ private :generate_url, :url_options include ActionView::Helpers::TextHelper def short_name(chars = 40) if self[:nickname].blank? - truncate self.name, :length => chars, :omission => '...' + if chars + truncate self.name, length: chars, omission: '...' + else + self.name + end else self[:nickname] end diff --git a/app/views/profile/_favorite_enterprise.html.erb b/app/views/profile/_favorite_enterprise.html.erb new file mode 100644 index 0000000..16ae381 --- /dev/null +++ b/app/views/profile/_favorite_enterprise.html.erb @@ -0,0 +1,15 @@ +
+ <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> +
+
+

+ <%= link_to activity.user.short_name(nil), activity.user.url %> <%= describe activity %> +

+

<%= time_ago_as_sentence activity.created_at %>

+ +
+ <%= link_to_function(_('Remove'), 'remove_item_wall(this, \'%s\', \'%s\', \'%s\'); return false ;' % [".profile-activity-item", url_for(:profile => params[:profile], :action => :remove_activity, :activity_id => activity.id, :view => params[:view]), _('Are you sure you want to remove this activity and all its replies?')]) if logged_in? && current_person == @profile %> +
+
+ +
diff --git a/config/initializers/action_tracker.rb b/config/initializers/action_tracker.rb index 7d7ab94..fbac0e3 100644 --- a/config/initializers/action_tracker.rb +++ b/config/initializers/action_tracker.rb @@ -40,6 +40,9 @@ ActionTrackerConfig.verbs = { remove_product: { }, + favorite_enterprise: { + }, + } ActionTrackerConfig.current_user = proc do diff --git a/db/migrate/20150310132902_add_id_to_favorite_enterprises_people.rb b/db/migrate/20150310132902_add_id_to_favorite_enterprises_people.rb new file mode 100644 index 0000000..3863765 --- /dev/null +++ b/db/migrate/20150310132902_add_id_to_favorite_enterprises_people.rb @@ -0,0 +1,26 @@ +class AddIdToFavoriteEnterprisesPeople < ActiveRecord::Migration + def up + rename_table :favorite_enteprises_people, :favorite_enterprise_people + + change_table :favorite_enterprise_people do |t| + t.timestamps + end + add_column :favorite_enterprise_people, :id, :primary_key + + add_index :favorite_enterprise_people, [:person_id, :enterprise_id] + add_index :favorite_enterprise_people, :person_id + add_index :favorite_enterprise_people, :enterprise_id + end + + def down + rename_table :favorite_enterprise_people, :favorite_enteprises_people + + remove_column :favorite_enteprises_people, :id + remove_column :favorite_enteprises_people, :created_at + remove_column :favorite_enteprises_people, :updated_at + + remove_index :favorite_enteprises_people, [:person_id, :enterprise_id] + remove_index :favorite_enteprises_people, :person_id + remove_index :favorite_enteprises_people, :enterprise_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 3aead80..1ba9598 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -351,11 +351,17 @@ ActiveRecord::Schema.define(:version => 20150712130827) do add_index "external_feeds", ["enabled"], :name => "index_external_feeds_on_enabled" add_index "external_feeds", ["fetched_at"], :name => "index_external_feeds_on_fetched_at" - create_table "favorite_enteprises_people", :id => false, :force => true do |t| - t.integer "person_id" - t.integer "enterprise_id" + create_table "favorite_enterprise_people", :force => true do |t| + t.integer "person_id" + t.integer "enterprise_id" + t.datetime "created_at" + t.datetime "updated_at" end + add_index "favorite_enterprise_people", ["enterprise_id"], :name => "index_favorite_enterprise_people_on_enterprise_id" + add_index "favorite_enterprise_people", ["person_id", "enterprise_id"], :name => "index_favorite_enterprise_people_on_person_id_and_enterprise_id" + add_index "favorite_enterprise_people", ["person_id"], :name => "index_favorite_enterprise_people_on_person_id" + create_table "friendships", :force => true do |t| t.integer "person_id" t.integer "friend_id" -- libgit2 0.21.2