履歴: ソフトウェア/rails/PaperTrail+ActiveScaffold

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 19 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2017/11/29 23:51:31ソフトウェア/rails/PaperTrail+ActiveScaffold 最新版65,862+1 -1表示差分—
2017/11/29 23:51:31ソフトウェア/rails/PaperTrail+ActiveScaffold 65,868+142 -40表示差分選択
2017/11/20 22:46:29ソフトウェア/rails/PaperTrail+ActiveScaffold 62,621+28 -7表示差分選択
2017/11/17 4:40:23ソフトウェア/rails/PaperTrail+ActiveScaffold 62,164+439 -168表示差分選択
2017/11/11 0:33:35ソフトウェア/rails/PaperTrail+ActiveScaffold 56,254+62 -0表示差分選択
2017/11/10 15:30:30ソフトウェア/rails/PaperTrail+ActiveScaffold 54,775+234 -0表示差分選択
2017/7/21 3:01:44ソフトウェア/rails/PaperTrail+ActiveScaffold 45,642+217 -0表示差分選択
2014/10/28 2:23:29ソフトウェア/rails/PaperTrail+ActiveScaffold 37,600+18 -1表示差分選択
2014/5/20 12:30:48ソフトウェア/rails/PaperTrail+ActiveScaffold 37,301+3 -1表示差分選択
2014/5/19 7:59:00ソフトウェア/rails/PaperTrail+ActiveScaffold 37,274+1 -1表示差分選択
2014/5/16 12:35:23ソフトウェア/rails/PaperTrail+ActiveScaffold 37,268+141 -13表示差分選択
2014/5/16 2:38:10ソフトウェア/rails/PaperTrail+ActiveScaffold 33,839+118 -1表示差分選択
2014/5/16 1:02:59ソフトウェア/rails/PaperTrail+ActiveScaffold 30,813+225 -26表示差分選択
2014/5/1 16:06:00ソフトウェア/rails/PaperTrail+ActiveScaffold 25,360+329 -0表示差分選択
2014/4/30 13:35:06ソフトウェア/rails/PaperTrail+ActiveScaffold 16,966+264 -0表示表示中選択
2014/4/28 1:10:44ソフトウェア/rails/PaperTrail+ActiveScaffold 10,805+4 -0表示差分基準
2014/4/25 2:51:34ソフトウェア/rails/PaperTrail+ActiveScaffold 10,732+2 -2表示差分選択
2014/4/24 15:21:55ソフトウェア/rails/PaperTrail+ActiveScaffold 10,728+382 -1表示差分選択
2014/4/24 11:01:35ソフトウェア/rails/PaperTrail+ActiveScaffold 1,131— —表示—選択

基準: ソフトウェア/rails/PaperTrail+ActiveScaffold (2014/4/28 1:10:44) → 比較: ソフトウェア/rails/PaperTrail+ActiveScaffold (2014/4/30 13:35:06)

@@ -421,6 +421,270 @@
acts_as_paranoid を入れて論理削除を使えばもっと簡単になるのかも。
+ということで、paranoia を入れてみました -> https://github.com/radar/paranoia
+
+1つ問題になるのは、~
+https://groups.google.com/forum/#!topic/activescaffold/_ocZQgVjLl8 ~
+にあるように、ActiveScaffold で default_scope を無視した表示がしにくいことのようです。
+
+** acts_as_paranoid の設定 [#ad305b63]
+
+ LANG:console
+ $ rails generate migration AddDeletedAtToTests deleted_at:datetime:index
+ $ rake db:migrate
+
+app/models/test.rb
+
+ LANG:ruby
+ class Test < ActiveRecord::Base
+ acts_as_paranoid
+ validates :email, presence: true, uniqueness: {scope: :deleted_at}
+
+ ...
+
+ end
+
+acts_as_paranoid と validates uniqueness を付け加え、
+削除と、削除済みを考慮した重複検出が行えていることを確認できました。
+
+** 削除済みレコードを表示する [#g0902b38]
+
+このままだと削除されたレコードが一覧に出てこないので、
+
+app/controllers/tests_controller.rb
+
+ LANG:ruby
+ class TestsController < ApplicationController
+
+ ...
+
+ def beginning_of_chain
+ super.unscoped
+ end
+
+ end
+
+とすることで、削除済みのアイテムを表示できました。
+
+** histories を一覧 [#s25fec03]
+
+上記だけだと histories のリンクで Request Failed となってしまいます。
+
+ LANG:console
+ Started GET "/tests/4/histories?association=histories&parent_scaffold=tests&adapter=_list_inline_adapter" for 192.168.133.1 at 2014-04-27 14:50:44 +0900
+ Processing by Test::HistoriesController#index as JS
+ Parameters: {"association"=>"histories", "parent_scaffold"=>"tests", "adapter"=>"_list_inline_adapter", "test_id"=>"4"}
+ Test Load (0.2ms) SELECT "tests".* FROM "tests" WHERE "tests"."deleted_at" IS NULL AND "tests"."id" = ? LIMIT 1 [["id", "4"]]
+ Completed 404 Not Found in 3ms
+
+ ActiveRecord::RecordNotFound (Couldn't find Test with id=4 [WHERE "tests"."deleted_at" IS NULL]):
+ activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:199:in `raise_record_not_found_exception!'
+ activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:285:in `find_one'
+ activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:269:in `find_with_ids'
+ activerecord (4.0.4) lib/active_record/relation/finder_methods.rb:36:in `find'
+ activerecord-deprecated_finders (1.0.3) lib/active_record/deprecated_finders/relation.rb:122:in `find'
+ activerecord (4.0.4) lib/active_record/querying.rb:3:in `find'
+ /home/osamu/.bundler/ruby/2.0.0/active_scaffold-6b895cd14a2a/lib/active_scaffold/finder.rb:325:in `find_if_allowed'
+ /home/osamu/.bundler/ruby/2.0.0/active_scaffold-6b895cd14a2a/lib/active_scaffold/actions/nested.rb:97:in `nested_parent_record'
+
+lib/active_scaffold/actions/nested.rb:97:in `nested_parent_record' で find_if_allowed
+に渡されている nested.parent_model にスコープが付いているのがいけないようなので、
+
+ LANG:ruby
+ class Test::HistoriesController < ApplicationController
+
+ ...
+
+ def nested_parent_record(crud = :read)
+ # @nested_parent_record ||= find_if_allowed(nested.parent_id, crud, nested.parent_model)
+ @nested_parent_record ||= find_if_allowed(nested.parent_id, crud, nested.parent_model.unscoped)
+ end
+
+ end
+
+としたところ、ちゃんと表示されるようになりました。
+
+&attachref(acts_as_paranoid.png,,50%);
+
+** delete 済みのレコードに edit と delete のアクションが表示される件 [#d048d220]
+
+2つの方法があるようです。
+
+*** authorized_for で無効にする [#u527a884]
+
+app/models/test.rb
+
+ LANG:ruby
+ class Test < ActiveRecord::Base
+ ...
+
+ def authorized_for_delete?
+ not deleted_at
+ end
+
+ def authorized_for_update?
+ not deleted_at
+ end
+
+ end
+
+とすることで、Edit や Delete の文字は残しつつ、
+グレーアウトさせることができました。
+
+*** skip_action_link? でフィルター [#t214ef36]
+
+アクションを完全に消すには、
+skip_action_link? でフィルターしてやればいいようです。
+
+呼び出される際は、args[0] にレコードが渡されたり渡されなかったりするので、
+渡された場合かつ deleted_at が null でない場合かつ crud_type が read 以外の場合に
+表示を非許可にしました。
+
+app/helpers/tests_helper.rb
+
+ LANG:ruby
+ module TestsHelper
+
+ ...
+
+ def skip_action_link?(link, *args)
+ (args.size > 0) and args[0].respond_to?(:deleted_at) and args[0].deleted_at and (link.crud_type!=:read)
+ end
+
+ end
+
+これで、削除済みのレコードに対してのみ Show 以外のアクションを完全に消せました。
+
+** 削除時にレコードが消えてしまう [#u96f4983]
+
+削除したレコードも表示して欲しいので、
+削除成功時には行を消去する代わりに表示し直すことにします。
+
+削除失敗時にはエラーメッセージを表示するため、普通に 'destroy' を呼びます。
+
+app/controllers/tests_controller.rb
+
+ class TestsController < ApplicationController
+ ...
+
+ protected
+
+ def destroy_respond_to_js
+ if successful?
+ render :action => 'update_row'
+ else
+ # エラーメッセージを表示する
+ render :action => 'destroy'
+ end
+ end
+
+ end
+
+** 削除済みのレコードに Restore アクションを追加する [#aa1a8d15]
+
+app/controllers/tests_controller.rb
+
+ LANG:ruby
+ class TestsController < ApplicationController
+ active_scaffold :"test" do |conf|
+ ...
+
+ conf.action_links.add 'restore', :label => 'Restore', :type => :member,
+ :confirm => 'Are you sure you want to restore the record?',
+ :method => :put, :position => :false
+ end
+ ...
+
+ def restore
+
+ end
+
+ end
+
+config/routes.rb
+
+ LANG:ruby
+ resources :tests do
+ member do
+ get 'restore'
+ end
+ as_routes
+ resources :histories, controller: 'test/histories' do
+ as_routes
+ end
+ end
+
+app/helpers/tests_helper.rb
+
+ LANG:ruby
+ module TestsHelper
+ ...
+
+ def skip_action_link?(link, *args)
+ return false unless (args.size > 0) and args[0].respond_to?(:deleted_at)
+
+ return true if args[0].deleted_at and (link.action=='destroy' or link.action=='edit')
+ return true if not args[0].deleted_at and (link.action=='restore')
+ end
+ end
+
+これで、削除済みのレコードにのみ Restore が表示されました。
+
+TestsController::restore の中身は
+active_scaffold/lib/active_scaffold/actions/update.rb
+を参考にして、以下のようにすれば良いようでした。
+
+app/controllers/tests_controller.rb
+
+ LANG:ruby
+ class TestsController < ApplicationController
+ ...
+
+ def restore
+ do_restore
+ respond_to_action(:restore)
+ end
+
+ protected
+
+ def do_restore
+ do_edit
+ @record.deleted_at = nil
+ update_save(no_record_param_update: true)
+ end
+
+ def restore_respond_to_js
+ update_row
+ end
+
+ def destroy_respond_to_js
+ # Update row instead of deleting
+ update_row
+ end
+
+ def update_row
+ if successful?
+ render :action => 'update_row'
+ else
+ # エラーメッセージを表示する
+ render :action => 'destroy'
+ end
+ end
+
+ end
+
+これで、
+
+- acts_as_paranoid による restore 可能な削除を実現
+- 作成・編集・削除・リストアすべてを paper_trail で追跡
+- 作成・編集・削除・リストア・履歴閲覧が ActiveScaffold 上で可能
+
+になりました。
+
+&attachref(acts_as_paranoid2.png,,50%);
+
+リストア時に uniqueness 制約に引っかかれば、ちゃんとエラーメッセージも表示されます。
+
* 質問・コメント [#t616f744]
#article_kcaptcha