track_changes 0.5.1 → 1.0.0.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/README.rdoc +46 -32
  2. data/TODO +1 -1
  3. data/VERSION.yml +4 -4
  4. data/lib/generators/track_changes/templates/initializer.rb +1 -0
  5. data/lib/generators/track_changes/templates/migration.rb +18 -0
  6. data/lib/generators/track_changes/templates/stylesheet.css +12 -0
  7. data/lib/generators/track_changes/track_changes_generator.rb +30 -0
  8. data/lib/track_changes/action_controller.rb +31 -0
  9. data/lib/track_changes/active_record.rb +26 -0
  10. data/lib/track_changes/around_save.rb +9 -0
  11. data/lib/track_changes/changes.rb +15 -0
  12. data/lib/track_changes/configuration.rb +3 -32
  13. data/lib/track_changes/current_user.rb +5 -0
  14. data/lib/track_changes/current_user_filter.rb +12 -0
  15. data/lib/track_changes/engine.rb +7 -0
  16. data/lib/track_changes.rb +11 -10
  17. data/test/functional/posts_controller_test.rb +39 -10
  18. data/test/railsapp/Gemfile +12 -0
  19. data/test/railsapp/Gemfile.lock +84 -0
  20. data/test/railsapp/Rakefile +4 -0
  21. data/test/{rails_root → railsapp}/app/controllers/application_controller.rb +1 -0
  22. data/test/railsapp/app/controllers/posts_controller.rb +40 -0
  23. data/test/railsapp/app/models/post.rb +3 -0
  24. data/test/railsapp/app/models/user.rb +3 -0
  25. data/test/railsapp/app/views/layouts/application.html.erb +12 -0
  26. data/test/railsapp/app/views/posts/_form.html.erb +13 -0
  27. data/test/railsapp/app/views/posts/_post.html.erb +3 -0
  28. data/test/railsapp/app/views/posts/edit.html.erb +5 -0
  29. data/test/railsapp/app/views/posts/index.html.erb +2 -0
  30. data/test/railsapp/config/application.rb +12 -0
  31. data/test/railsapp/config/boot.rb +13 -0
  32. data/test/railsapp/config/database.yml +5 -0
  33. data/test/railsapp/config/environment.rb +5 -0
  34. data/test/railsapp/config/environments/test.rb +10 -0
  35. data/test/railsapp/config/routes.rb +3 -0
  36. data/test/railsapp/config.ru +2 -0
  37. data/test/railsapp/db/test.sqlite3 +0 -0
  38. data/test/railsapp/log/test.log +6385 -0
  39. data/test/railsapp/script/rails +6 -0
  40. data/test/schema.rb +19 -0
  41. data/test/test_helper.rb +4 -26
  42. data/test/track_changes_test.rb +34 -0
  43. metadata +124 -88
  44. data/lib/track_changes/audit_filter.rb +0 -69
  45. data/lib/track_changes/base.rb +0 -9
  46. data/lib/track_changes/class_methods.rb +0 -74
  47. data/lib/track_changes/filter.rb +0 -43
  48. data/lib/track_changes/initializer.rb +0 -18
  49. data/lib/track_changes/instance_methods.rb +0 -5
  50. data/lib/track_changes/result.rb +0 -36
  51. data/test/base_test.rb +0 -20
  52. data/test/class_methods_test.rb +0 -29
  53. data/test/filter_test.rb +0 -73
  54. data/test/rails_root/app/controllers/posts_controller.rb +0 -36
  55. data/test/rails_root/app/models/audit.rb +0 -4
  56. data/test/rails_root/app/models/post.rb +0 -3
  57. data/test/rails_root/config/boot.rb +0 -110
  58. data/test/rails_root/config/database.yml +0 -3
  59. data/test/rails_root/config/environment.rb +0 -7
  60. data/test/rails_root/config/environments/test.rb +0 -7
  61. data/test/rails_root/config/initializers/new_rails_defaults.rb +0 -7
  62. data/test/rails_root/config/initializers/session_store.rb +0 -4
  63. data/test/rails_root/config/initializers/track_changes_setup.rb +0 -8
  64. data/test/rails_root/config/routes.rb +0 -3
  65. data/test/rails_root/db/migrate/20100115021125_create_audits.rb +0 -16
  66. data/test/rails_root/db/migrate/20100115021151_create_posts.rb +0 -15
  67. data/test/rails_root/log/test.log +0 -543
  68. data/test/rails_root/script/console +0 -3
  69. data/test/rails_root/script/generate +0 -3
  70. data/test/result_test.rb +0 -35
  71. data/test/track_changes/audit_filter_test.rb +0 -154
  72. data/test/track_changes/configuration_test.rb +0 -36
  73. data/test/track_changes/initializer_test.rb +0 -37
data/README.rdoc CHANGED
@@ -1,60 +1,72 @@
1
1
  = track_changes
2
2
 
3
- TrackChanges is a Rails plugin to facilitate tracking
4
- changes made to an ActiveRecord model in your
5
- Controller actions.
3
+ TrackChanges is a Rails engine to facilitate tracking changes made to your
4
+ models.
6
5
 
7
6
  == Why?
8
7
 
9
- I originally looked at the available auditing solutions
10
- and it appeared that most of them were not thread-safe.
8
+ I originally looked at the available auditing solutions and it appeared that
9
+ most of them were not thread-safe.
10
+
11
+ == Requirements
12
+
13
+ * Rails 3
14
+ * A User model
15
+
16
+ === Development Requirements
17
+
18
+ * JRuby
19
+ * Rails 3
11
20
 
12
21
  == Installation
13
22
 
14
- gem install track_changes
23
+ Add to your Gemfile:
24
+
25
+ gem 'track_changes'
26
+
27
+ Then, you can run <tt>rails generate track_changes</tt> to create the migration
28
+ file, the initializer, and copy the CSS stylesheet to your application.
15
29
 
16
30
  == Configuration
17
31
 
18
- You need to have an Audit class that has a polymorphic
19
- <tt>belongs_to</tt> association to <tt>:audited</tt>. In
20
- addition, the Audit model must have a <tt>user</tt>
21
- attribute, and your controllers must respond to
22
- <tt>current_user</tt>.
32
+ Currently, the only configuration available is in the initializer
33
+ <tt>config/initializers/track_changes_configuration.rb</tt>. In this file, you
34
+ can set a constant TrackChanges::Configuration::DEFAULT_USER_FINDER to a Proc
35
+ that will return a User model when no <tt>current_user</tt> is specified.
23
36
 
24
- Example Audit class:
37
+ == Model Example
25
38
 
26
- class Audit < ActiveRecord::Base
27
- belongs_to :audited, :polymorphic => true
28
- serialize :change_set
29
- end
39
+ To enable tracking for your model, add the statement <tt>track_changes</tt> in
40
+ your model definition.
30
41
 
31
- Example Audited class:
42
+ Example:
32
43
 
33
44
  class Post < ActiveRecord::Base
34
- has_many :audits, :as => :audited
45
+ track_changes
35
46
  end
36
47
 
37
- You can also tweak some of the defaults by creating an initializer
38
- in <tt>RAILS_ROOT/config/initializers/track_changes_configuration.rb</tt>
39
- and calling TrackChanges::Initializer.instance which yields a
40
- TrackChanges::Configuration object:
41
-
42
- # These are the defaults anyways
43
- TrackChanges::Initializer.instance do |c|
44
- c.audit_assocation = :audits # Calls <tt>create!</tt> on this association method
45
- c.current_user = :current_user # Controller is sent this method to obtain current user.
46
- c.ignore_nil = true # Don't crash if a model is nil.
47
- end
48
+ This will add a polymorphic <tt>has_many</tt> association to the Audit class. It
49
+ will also add an accessor <tt>current_user</tt> which you can set prior to
50
+ updating your model. This will be saved in the Audit entry.
48
51
 
49
52
  == Controller Example
50
53
 
54
+ To enable automatic user tracking in your controllers. Add the <tt>track_changes</tt>
55
+ statement to your controller. Pass a Symbol as an argument which is the name
56
+ of the instance variable(s) (without the <tt>@</tt>). Theses instance variables
57
+ will have their <tt>current_user</tt> attribute assigned to via the controller's
58
+ <tt>current_user</tt> method.
59
+
60
+ The <tt>track_changes</tt> method in your controller will also pull in the
61
+ AuditsHelper module which provides some simple helpers.
62
+
63
+ Example:
64
+
51
65
  In this example, after the `update` action is called,
52
66
  the `@post` will be compared to a previous version, and
53
67
  if there are any changes, an audit will be created.
54
68
 
55
69
  class PostController < ApplicationController
56
- include TrackChanges
57
-
58
70
  before_filter :get_post, :except => [:index, :new]
59
71
 
60
72
  # specify a single model
@@ -62,7 +74,7 @@ if there are any changes, an audit will be created.
62
74
 
63
75
  # you can also specify multiple models
64
76
  #track_changes :post1, :post2, ...
65
-
77
+
66
78
  def update
67
79
  if @post.update_attributes(params[:post])
68
80
  flash[:notice] = "Success."
@@ -81,6 +93,8 @@ if there are any changes, an audit will be created.
81
93
  end
82
94
  end
83
95
 
96
+ Note, you must have a <tt>before_filter</tt> that assigns the model to the expected
97
+ instance variable.
84
98
 
85
99
  == COPYRIGHT
86
100
 
data/TODO CHANGED
@@ -1,4 +1,4 @@
1
1
  Track Changes TODO List
2
2
  =======================
3
3
 
4
- * Check for ivars
4
+ * Documentation update for re-write
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 1
3
- :major: 0
4
- :build:
5
- :minor: 5
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 0
5
+ :build: pre1
@@ -0,0 +1 @@
1
+ TrackChanges::Configuration::DEFAULT_USER_FINDER = Proc.new { User.first }
@@ -0,0 +1,18 @@
1
+ class CreateAuditsTable < ActiveRecord::Migration #:nodoc:
2
+ def self.up
3
+ create_table :audits do |t|
4
+ t.references :audited, :polymorphic => true, :null => false
5
+ t.references :user
6
+ t.text :change_set, :null => false
7
+
8
+ t.datetime :created_at, :null => false
9
+ end
10
+
11
+ add_index :audits, [:audited_id, :audited_type]
12
+ add_index :audits, [:user_id]
13
+ end
14
+
15
+ def self.down
16
+ drop_table :audits
17
+ end
18
+ end
@@ -0,0 +1,12 @@
1
+ .audit {
2
+ font-size: 12px;
3
+ }
4
+
5
+ li.audit {
6
+ list-style: none;
7
+ }
8
+
9
+ h4.audit div { display: inline; }
10
+ .audit_meta { font-weight: normal; color: #666; }
11
+
12
+ p.audit { line-height: 10px; margin-left: 13px;}
@@ -0,0 +1,30 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class TrackChangesGenerator < Rails::Generators::Base #:nodoc:
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.expand_path("templates", File.dirname(__FILE__))
8
+
9
+ # Implement the required interface for Rails::Generators::Migration.
10
+ # taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
11
+ def self.next_migration_number(dirname)
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ else
15
+ "%.3d" % (current_migration_number(dirname) + 1)
16
+ end
17
+ end
18
+
19
+ def create_migration_file
20
+ migration_template 'migration.rb', 'db/migrate/create_audits_table.rb'
21
+ end
22
+
23
+ def generate_layout
24
+ copy_file 'stylesheet.css', 'public/stylesheets/track_changes.css'
25
+ end
26
+
27
+ def create_initializer_file
28
+ copy_file 'initializer.rb', 'config/initializers/track_changes_configuration.rb'
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ module TrackChanges
2
+ module ActionController #:nodoc:
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ # Sets up an around filter to assign the controller's <tt>current_user</tt>
10
+ # to the given model names that are already set as instance variables in a
11
+ # prior <tt>before_filter</tt>.
12
+ #
13
+ # Example:
14
+ #
15
+ # track_changes :post
16
+ # track_changes :post, :post_attribute
17
+ #
18
+ # Currently does not work if the instance variable is anything except
19
+ # an model that has <tt>current_user</tt> accessors.
20
+ def track_changes models
21
+ models_to_track = [models].flatten
22
+ define_method(:__track_changes_to_models) { models_to_track }
23
+
24
+ self.class_eval do
25
+ helper :audits
26
+ before_filter TrackChanges::CurrentUserFilter
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ module TrackChanges
2
+ module ActiveRecord #:nodoc:
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ # Enables auditing of all changes to an ActiveRecord model. Sets up an
10
+ # around filter that will create an Audit for the models <tt>changes</tt>
11
+ # attribute.
12
+ #
13
+ # In addition, this will also define a <tt>attr_accessor</tt> for <tt>current_user</tt>.
14
+ def track_changes
15
+ send :include, TrackChanges::CurrentUser
16
+ send :include, TrackChanges::Changes
17
+
18
+ self.class_eval do
19
+ has_many :audits, :as => :audited
20
+
21
+ around_save TrackChanges::AroundSave
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module TrackChanges
2
+ class AroundSave #:nodoc:
3
+ def self.around_save(model)
4
+ model.send(:track_changes_get_changes)
5
+ yield
6
+ model.send(:track_changes_save_audit)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module TrackChanges
2
+ module Changes #:nodoc:
3
+
4
+ protected
5
+
6
+ def track_changes_get_changes
7
+ @__model_changes = self.changes
8
+ end
9
+
10
+ def track_changes_save_audit
11
+ user = current_user || TrackChanges::Configuration::get_default_user
12
+ Audit.create_changes(self, @__model_changes, current_user)
13
+ end
14
+ end
15
+ end
@@ -1,38 +1,9 @@
1
1
  module TrackChanges
2
2
  class Configuration
3
- # The association used to create an audit. Defaults to
4
- # <tt>:audits</tt>
5
- #
6
- # Example:
7
- # :audits # => Call model.audits.create
8
- # :changes # => Call model.changes.create
9
- attr_accessor :audit_association
10
3
 
11
- # The controller method called to obtain the current user.
12
- # Defaults to <tt>:current_user</tt>
13
- attr_accessor :current_user
14
-
15
- # Ignore if the audited item is nil. Defaults to <tt>true</tt>.
16
- attr_accessor :ignore_nil
17
-
18
- def initialize
19
- self.audit_association = default_audit_association
20
- self.current_user = default_current_user
21
- self.ignore_nil = default_ignore_nil
22
- end
23
-
24
- private
25
-
26
- def default_audit_association
27
- :audits
28
- end
29
-
30
- def default_current_user
31
- :current_user
32
- end
33
-
34
- def default_ignore_nil
35
- true
4
+ # If a constant DEFAULT_USER_FINDER is defined in this class. Call it.
5
+ def self.get_default_user
6
+ defined?(DEFAULT_USER_FINDER) && DEFAULT_USER_FINDER.call
36
7
  end
37
8
  end
38
9
  end
@@ -0,0 +1,5 @@
1
+ module TrackChanges
2
+ module CurrentUser #:nodoc:
3
+ attr_accessor :current_user
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ module TrackChanges
2
+ module CurrentUserFilter #:nodoc:
3
+ def self.filter(controller)
4
+ controller.send(:__track_changes_to_models).each do |model|
5
+ instance = controller.instance_variable_get("@#{model}")
6
+ return if instance.nil?
7
+
8
+ instance.current_user = controller.send(:current_user)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ require 'track_changes'
2
+ require 'rails'
3
+
4
+ module TrackChanges
5
+ class Engine < Rails::Engine #:nodoc:
6
+ end
7
+ end
data/lib/track_changes.rb CHANGED
@@ -1,12 +1,13 @@
1
- require 'track_changes/result'
2
- require 'track_changes/configuration'
3
- require 'track_changes/initializer'
4
- require 'track_changes/audit_filter'
5
- require 'track_changes/filter'
6
- require 'track_changes/instance_methods'
7
- require 'track_changes/class_methods'
8
- require 'track_changes/base'
9
-
10
1
  module TrackChanges
11
-
2
+ require 'track_changes/engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
3
+ require 'track_changes/action_controller'
4
+ require 'track_changes/active_record'
5
+ require 'track_changes/around_save'
6
+ require 'track_changes/changes'
7
+ require 'track_changes/configuration'
8
+ require 'track_changes/current_user'
9
+ require 'track_changes/current_user_filter'
12
10
  end
11
+
12
+ ActiveRecord::Base.send :include, TrackChanges::ActiveRecord
13
+ ActionController::Base.send :include, TrackChanges::ActionController
@@ -1,16 +1,45 @@
1
- require 'test_helper'
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  class PostsControllerTest < ActionController::TestCase
4
- test "should update post" do
5
- post = Post.create!(:title => "Hello, World", :body => "This is a test.", :author => "Matt Haley")
6
- assert !post.new_record?
4
+ load_schema
7
5
 
8
- assert_difference("Audit.count") do
9
- put :update, :id => post.to_param, :post => { :title => "First post!" }
10
- assert_redirected_to post_path(assigns(:post))
6
+ def setup
7
+ @post = Post.create(:title => "Hello", :body => "Hello World")
8
+ end
9
+
10
+ def test_should_audit_changes
11
+ put :update, :id => @post.to_param, :post => { :title => "Changed" }
12
+
13
+ @post.reload
14
+ expected_change = { "title" => ["Hello", "Changed"]}
15
+ assert_equal "Changed", @post.title
16
+ assert_equal 2, @post.audits.count
17
+ assert_equal expected_change, @post.audits.desc.first.change_set
18
+ end
19
+
20
+ def test_should_respect_current_user
21
+ User.delete_all
22
+ @user = User.create!(:name => "ControllerTest")
23
+ User.create!(:name => "Someone Else")
24
+
25
+ put :update, :id => @post.to_param, :post => { :title => "Changed User Test"},
26
+ :user_id => @user.id
27
+
28
+ @post.reload
29
+ audit = @post.audits.desc.first
30
+
31
+ assert_equal @user.id, audit.user_id
32
+ end
33
+
34
+ def test_should_not_fail_if_current_user_nil
35
+ User.delete_all
36
+
37
+ put :update, :id => @post.to_param, :post => { :title => "Changed User Test"},
38
+ :user_id => nil
39
+
40
+ @post.reload
41
+ audit = @post.audits.desc.first
11
42
 
12
- audit = assigns(:post).audits.first
13
- assert_equal ["Hello, World", "First post!"], audit.change_set["title"]
14
- end
43
+ assert_equal nil, audit.user_id
15
44
  end
16
45
  end
@@ -0,0 +1,12 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'rails', '3.0.0.rc'
4
+
5
+ if defined?(JRUBY_VERSION)
6
+ gem 'activerecord-jdbc-adapter'
7
+ gem 'activerecord-jdbcsqlite3-adapter', :require => false
8
+ else
9
+ gem 'sqlite3-ruby'
10
+ end
11
+
12
+ gem 'track_changes', :path => "../.."
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: C:/Users/Matt/Code/track_changes
3
+ specs:
4
+ track_changes (0.5.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ abstract (1.0.0)
10
+ actionmailer (3.0.0.rc)
11
+ actionpack (= 3.0.0.rc)
12
+ mail (~> 2.2.5)
13
+ actionpack (3.0.0.rc)
14
+ activemodel (= 3.0.0.rc)
15
+ activesupport (= 3.0.0.rc)
16
+ builder (~> 2.1.2)
17
+ erubis (~> 2.6.6)
18
+ i18n (~> 0.4.1)
19
+ rack (~> 1.2.1)
20
+ rack-mount (~> 0.6.9)
21
+ rack-test (~> 0.5.4)
22
+ tzinfo (~> 0.3.22)
23
+ activemodel (3.0.0.rc)
24
+ activesupport (= 3.0.0.rc)
25
+ builder (~> 2.1.2)
26
+ i18n (~> 0.4.1)
27
+ activerecord (3.0.0.rc)
28
+ activemodel (= 3.0.0.rc)
29
+ activesupport (= 3.0.0.rc)
30
+ arel (~> 0.4.0)
31
+ tzinfo (~> 0.3.22)
32
+ activerecord-jdbc-adapter (0.9.7-java)
33
+ activerecord-jdbcsqlite3-adapter (0.9.7-java)
34
+ activerecord-jdbc-adapter (= 0.9.7)
35
+ jdbc-sqlite3 (>= 3.6.3.054)
36
+ activeresource (3.0.0.rc)
37
+ activemodel (= 3.0.0.rc)
38
+ activesupport (= 3.0.0.rc)
39
+ activesupport (3.0.0.rc)
40
+ arel (0.4.0)
41
+ activesupport (>= 3.0.0.beta)
42
+ builder (2.1.2)
43
+ erubis (2.6.6)
44
+ abstract (>= 1.0.0)
45
+ i18n (0.4.1)
46
+ jdbc-sqlite3 (3.6.3.054)
47
+ mail (2.2.5)
48
+ activesupport (>= 2.3.6)
49
+ mime-types
50
+ treetop (>= 1.4.5)
51
+ mime-types (1.16)
52
+ polyglot (0.3.1)
53
+ rack (1.2.1)
54
+ rack-mount (0.6.9)
55
+ rack (>= 1.0.0)
56
+ rack-test (0.5.4)
57
+ rack (>= 1.0)
58
+ rails (3.0.0.rc)
59
+ actionmailer (= 3.0.0.rc)
60
+ actionpack (= 3.0.0.rc)
61
+ activerecord (= 3.0.0.rc)
62
+ activeresource (= 3.0.0.rc)
63
+ activesupport (= 3.0.0.rc)
64
+ bundler (>= 1.0.0.rc.1)
65
+ railties (= 3.0.0.rc)
66
+ railties (3.0.0.rc)
67
+ actionpack (= 3.0.0.rc)
68
+ activesupport (= 3.0.0.rc)
69
+ rake (>= 0.8.3)
70
+ thor (~> 0.14.0)
71
+ rake (0.8.7)
72
+ thor (0.14.0)
73
+ treetop (1.4.8)
74
+ polyglot (>= 0.3.1)
75
+ tzinfo (0.3.22)
76
+
77
+ PLATFORMS
78
+ java
79
+
80
+ DEPENDENCIES
81
+ activerecord-jdbc-adapter
82
+ activerecord-jdbcsqlite3-adapter
83
+ rails (= 3.0.0.rc)
84
+ track_changes!
@@ -0,0 +1,4 @@
1
+ require File.expand_path('../config/application', __FILE__)
2
+ require 'rake'
3
+
4
+ Railsapp::Application.load_tasks
@@ -1,2 +1,3 @@
1
1
  class ApplicationController < ActionController::Base
2
+ protect_from_forgery
2
3
  end
@@ -0,0 +1,40 @@
1
+ class PostsController < ApplicationController
2
+ before_filter :get_user
3
+ before_filter :get_post
4
+
5
+ track_changes :post
6
+
7
+ def index
8
+ @posts = Post.all
9
+ end
10
+
11
+ def edit
12
+
13
+ end
14
+
15
+ def update
16
+ if @post.update_attributes(params[:post])
17
+ flash[:notice] = "Post updated"
18
+ redirect_to posts_url
19
+ else
20
+ render :action => "edit"
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def get_post
27
+ Rails.logger.debug("get_post")
28
+ @post = Post.find(params[:id])
29
+ end
30
+
31
+ def get_user
32
+ Rails.logger.debug("get_user")
33
+ @user = User.find_by_id(params[:user_id])
34
+ end
35
+
36
+ def current_user
37
+ Rails.logger.debug("current_user")
38
+ @user
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+ track_changes
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+
3
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Railsapp</title>
5
+ <%= csrf_meta_tag %>
6
+ </head>
7
+ <body>
8
+
9
+ <%= yield %>
10
+
11
+ </body>
12
+ </html>
@@ -0,0 +1,13 @@
1
+ <p>
2
+ <b><%= form.label :title %></b><br />
3
+ <%= form.text_field :title %>
4
+ </p>
5
+
6
+ <p>
7
+ <b><%= form.label :body %></b><br />
8
+ <%= form.text_area :body%>
9
+ </p>
10
+
11
+ <p>
12
+ <%= form.submit "Save" %>
13
+ </p>
@@ -0,0 +1,3 @@
1
+ <h2><%= post.title %></h2>
2
+ <p><%= post.body %></p>
3
+ <p><%= link_to 'Edit', edit_post_path(post) %></p>
@@ -0,0 +1,5 @@
1
+ <h1>Edit Post</h1>
2
+
3
+ <%= form_for @post do |form| %>
4
+ <%= render form %>
5
+ <% end %>
@@ -0,0 +1,2 @@
1
+ <h1>Posts</h1>
2
+ <%= render @posts %>
@@ -0,0 +1,12 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
6
+
7
+ module Railsapp
8
+ class Application < Rails::Application
9
+ config.action_view.javascript_expansions[:defaults] = %w()
10
+ config.encoding = "utf-8"
11
+ end
12
+ end