rails-footnotes和current_user

时间:2015-03-26 14:22:08

标签: ruby-on-rails footnotes

我只想在用户是管理员的情况下显示备注。官方生成的initilizer具有以下行:

  # Whether or not to enable footnotes
  f.enabled = Rails.env.development?
  # You can also use a lambda / proc to conditionally toggle footnotes
  # Example :
  # f.enabled = -> { User.current.admin? }
  # Beware of thread-safety though, Footnotes.enabled is NOT thread safe
  # and should not be modified anywhere else.

但我做得不比这更好:

f.enabled = true
f.before {|controller, filter| filter.notes = controller.current_user.try(:admin?) ? [:controller, :view, :layout, :partials, :stylesheets, :javascripts, :assigns, :session, :cookies, :params, :filters, :routes, :env, :queries, :log] : []  }

它有效但是它是正确/安全的吗?我真正想要的是通过清空或不清除笔记列表来模拟rails-footnots。我怎样才能使用官方lambda表示法? User.current.admin?对我没有意义,因为模型不应该有权访问会话

1 个答案:

答案 0 :(得分:1)

打开issue后,

rails-footnotes已修复/改进。现在你可以这样做:

f.enabled = proc { |controller| controller.current_user.try(:admin?) }