Permalink
Browse files

switched to ERB

  • Loading branch information...
1 parent 4b5f347 commit 623716a73e870308089146b28be2663ac634cd28 @sbower sbower committed with pokonski Oct 18, 2011
View
@@ -4,7 +4,6 @@ gemspec
gem 'rails'
gem 'yard'
-gem 'i18n'
gem 'pusher'
group :development, :test do
View
@@ -10,43 +10,42 @@ PATH
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.1.0)
- actionpack (= 3.1.0)
+ actionmailer (3.1.1)
+ actionpack (= 3.1.1)
mail (~> 2.3.0)
- actionpack (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
+ actionpack (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
- rack-cache (~> 1.0.3)
+ rack-cache (~> 1.1)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
- sprockets (~> 2.0.0)
- activemodel (3.1.0)
- activesupport (= 3.1.0)
- bcrypt-ruby (~> 3.0.0)
+ sprockets (~> 2.0.2)
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
- activerecord (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
+ activerecord (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
- activeresource (3.1.0)
- activemodel (= 3.1.0)
- activesupport (= 3.1.0)
- activesupport (3.1.0)
+ activeresource (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ activesupport (3.1.1)
multi_json (~> 1.0)
arel (2.2.1)
- bcrypt-ruby (3.0.0)
builder (3.0.0)
crack (0.1.8)
diff-lcs (1.1.3)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
+ json (1.6.1)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -59,49 +58,50 @@ GEM
multi_json (~> 1.0)
ruby-hmac (~> 0.4.0)
signature (~> 0.1.2)
- rack (1.3.2)
- rack-cache (1.0.3)
+ rack (1.3.4)
+ rack-cache (1.1)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
- rails (3.1.0)
- actionmailer (= 3.1.0)
- actionpack (= 3.1.0)
- activerecord (= 3.1.0)
- activeresource (= 3.1.0)
- activesupport (= 3.1.0)
+ rails (3.1.1)
+ actionmailer (= 3.1.1)
+ actionpack (= 3.1.1)
+ activerecord (= 3.1.1)
+ activeresource (= 3.1.1)
+ activesupport (= 3.1.1)
bundler (~> 1.0)
- railties (= 3.1.0)
- railties (3.1.0)
- actionpack (= 3.1.0)
- activesupport (= 3.1.0)
+ railties (= 3.1.1)
+ railties (3.1.1)
+ actionpack (= 3.1.1)
+ activesupport (= 3.1.1)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
- rdoc (3.9.4)
- rspec (2.6.0)
- rspec-core (~> 2.6.0)
- rspec-expectations (~> 2.6.0)
- rspec-mocks (~> 2.6.0)
- rspec-core (2.6.4)
- rspec-expectations (2.6.0)
+ rdoc (3.11)
+ json (~> 1.4)
+ rspec (2.7.0)
+ rspec-core (~> 2.7.0)
+ rspec-expectations (~> 2.7.0)
+ rspec-mocks (~> 2.7.0)
+ rspec-core (2.7.0)
+ rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
- rspec-mocks (2.6.0)
- rspec-rails (2.6.1)
+ rspec-mocks (2.7.0)
+ rspec-rails (2.7.0)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
- rspec (~> 2.6.0)
+ rspec (~> 2.7.0)
ruby-hmac (0.4.0)
signature (0.1.2)
ruby-hmac
- sprockets (2.0.0)
+ sprockets (2.0.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
@@ -111,14 +111,13 @@ GEM
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
- tzinfo (0.3.29)
- yard (0.7.2)
+ tzinfo (0.3.30)
+ yard (0.7.3)
PLATFORMS
ruby
DEPENDENCIES
- i18n
public_activity!
pusher
rails
View
@@ -57,15 +57,18 @@ And in your views:
<%= activity.text %><br/>
<% end %>
-The only thing left is to add translations to your locale files, for example:
+The only thing left is to add templates (config/pba.yml), for example:
- en:
activity:
article:
create: 'Article has been created'
update: 'Someone has edited the article'
destroy: 'Some user removed an article!'
+Place this in a file and reference it in a rail initializer.
+
+ PublicActivity::Activity.template = YAML.load_file("#{RAILS_ROOT}/config/pba.yml")
+
This is only a basic example, refer to documentation for more options and customization!
## Documentation
View
@@ -48,7 +48,7 @@ module PublicActivity
included do
include Tracked
- include Activist
+ include Activist
end
end
@@ -1,5 +1,5 @@
require 'active_record'
-require 'i18n'
+
module PublicActivity
# The ActiveRecord model containing
# details about recorded activity.
@@ -11,22 +11,21 @@ class Activity < ActiveRecord::Base
# Serialize parameters Hash
serialize :parameters, Hash
- # Virtual attribute returning already
- # translated key with params passed
- # to i18n.translate function. You can pass additional Hash
- # you want to be passed to translation method. It will be merged with the default ones.
+ class_attribute :template
+
+ # Virtual attribute returning text description of the activity
+ # using basic ERB templating
#
# == Example:
#
# Let's say you want to show article's title inside Activity message.
#
- # #config/locales/en.yml
- # en:
- # activity:
- # article:
- # create: "Someone has created an article '%{title}'"
- # update: "Article '%{title}' has been modified"
- # destroy: "Someone deleted article '%{title}'!"
+ # #config/pba.yml
+ # activity:
+ # article:
+ # create: "New <%= trackable.name %> article has been created"
+ # update: 'Someone modified the article'
+ # destroy: 'Someone deleted the article!'
#
# And in controller:
#
@@ -40,24 +39,27 @@ class Activity < ActiveRecord::Base
# Now when you list articles, you should see:
# @article.activities.last.text #=> "Someone has created an article 'Rails 3.0.5 released!'"
def text(params = {})
-
- #make all the fields of the owner and trackable object automatically avaialable
-
- if !self.trackable.nil?
- self.trackable.attributes.each do |key, value|
- params[key.to_sym] = value
- end
- end
-
- if !self.owner.nil?
- self.owner.attributes.each do |key, value|
- params[key.to_sym] = value
- end
+ if !self.template.nil?
+ parameters.merge! params
+ renderer = ERB.new(resolveTemplate(key))
+ renderer.result(binding)
+ else
+ "Template not defined"
end
-
- parameters.merge! params
-
- I18n.t(key, parameters || {})
end
+
+ private
+ def resolveTemplate(key)
+ res = nil
+ key.split(".").each do |k|
+ if res.nil?
+ res = self.template[k]
+ else
+ res = res[k]
+ end
+ end
+ res
+ end
+
end
end
View
@@ -2,7 +2,6 @@
create_table :categories, :force => true do |t|
t.column :name, :string
- t.column :organization_id, :integer
end
create_table :departments, :force => true do |t|
@@ -11,12 +10,7 @@
create_table :notes, :force => true do |t|
t.column :body, :text
- t.column :notable_id, :integer
- t.column :notable_type, :string
- end
-
- create_table :things, :force => true do |t|
- t.column :body, :text
+ t.column :category_id, :integer
end
create_table :activities, :force => true do |t|
View
@@ -0,0 +1,8 @@
+note1:
+ id: 1
+ body: Note 1
+ category_id: 1
+note2:
+ id: 2
+ body: Note 2
+ category_id: 2
@@ -2,7 +2,7 @@
describe "public_activity" do
before(:all) do
- self.class.fixtures :categories, :departments
+ self.class.fixtures :categories, :departments, :notes
end
describe "tracked" do
@@ -43,7 +43,7 @@
it "should translate" do
@category.activities.last.text.should_not be_nil
- @category.activities.last.text.should == "New category has been created"
+ @category.activities.last.text.should == "New test cat category has been created"
end
it "should track deletes" do
@@ -53,13 +53,21 @@
end.should change(PublicActivity::Activity, :count).by(1)
end
- it "should track update" do
+ it "should track updates" do
lambda do
@category.name = "new cat"
@category.save
@category.activities.last.text.should == "Someone modified the category"
- end.should change(PublicActivity::Activity, :count).by(1)
+ end.should change(PublicActivity::Activity, :count).by(1)
+ end
+
+ it "should evaluate associations" do
+ note = Note.find(1)
+ note.body = "New Test"
+ note.save
+ note.activities.last.text.should == "Someone modified note New Test with category Category 1"
end
+
end
end
@@ -76,4 +84,12 @@
@department.activities.should_not be_nil
end
end
+
+ describe "empty template" do
+ it "should not die on nil" do
+ PublicActivity::Activity.template = nil
+ @category = Category.create(name: "test cat")
+ @category.activities.last.text.should == "Template not defined"
+ end
+ end
end
View
@@ -13,6 +13,7 @@
require 'action_controller'
require 'public_activity'
+PublicActivity::Activity.template = YAML.load_file(plugin_test_dir + "/support/pba.yml")
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
View
@@ -7,3 +7,7 @@ class Category < ActiveRecord::Base
validates_presence_of :name
end
+class Note < ActiveRecord::Base
+ tracked
+ belongs_to :category
+end
View
@@ -0,0 +1,9 @@
+activity:
+ category:
+ create: "New <%= trackable.name %> category has been created"
+ update: 'Someone modified the category'
+ destroy: 'Someone deleted the category!'
+ note:
+ create: "New note <%= trackable.body %> has been created"
+ update: 'Someone modified note <%= trackable.body %> with category <%= trackable.category.name %>'
+ destroy: 'Someone deleted the note!'

0 comments on commit 623716a

Please sign in to comment.