Module: ActiveAdmin::Resource::ScopeTo
- Included in:
- ActiveAdmin::Resource
- Defined in:
- lib/active_admin/resource/scope_to.rb
Instance Method Summary collapse
-
#scope_to(*args, &block) ⇒ Object
Scope this controller to some object which has a relation to the resource.
- #scope_to?(context = nil) ⇒ Boolean
- #scope_to_association_method ⇒ Object
- #scope_to_config ⇒ Object
- #scope_to_method ⇒ Object
Instance Method Details
#scope_to(*args, &block) ⇒ Object
Scope this controller to some object which has a relation to the resource. Can either accept a block or a symbol of a method to call.
Eg:
ActiveAdmin.register Post do
scope_to :current_user
end
Then every time we instantiate and object, it would call
current_user.posts.build
By default Active Admin will use the resource name to build a method to call as the association. If its different, you can pass in the association_method as an option.
scope_to :current_user, association_method: :blog_posts
will result in the following
current_user.blog_posts.build
To conditionally use this scope, you can use conditional procs
scope_to :current_user, if: proc{ admin_user_signed_in? }
or
scope_to :current_user, unless: proc{ current_user.admin? }
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_admin/resource/scope_to.rb', line 38 def scope_to(*args, &block) = args. method = args.first scope_to_config[:method] = block || method scope_to_config[:association_method] = [:association_method] scope_to_config[:if] = [:if] scope_to_config[:unless] = [:unless] end |
#scope_to?(context = nil) ⇒ Boolean
66 67 68 69 70 71 |
# File 'lib/active_admin/resource/scope_to.rb', line 66 def scope_to?(context = nil) return false if scope_to_method.nil? return render_in_context(context, scope_to_config[:if]) unless scope_to_config[:if].nil? return !render_in_context(context, scope_to_config[:unless]) unless scope_to_config[:unless].nil? true end |
#scope_to_association_method ⇒ Object
49 50 51 |
# File 'lib/active_admin/resource/scope_to.rb', line 49 def scope_to_association_method scope_to_config[:association_method] end |
#scope_to_config ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/active_admin/resource/scope_to.rb', line 57 def scope_to_config @scope_to_config ||= { method: nil, association_method: nil, if: nil, unless: nil } end |
#scope_to_method ⇒ Object
53 54 55 |
# File 'lib/active_admin/resource/scope_to.rb', line 53 def scope_to_method scope_to_config[:method] end |