diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-12-25 11:45:53 +0400 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-12-25 11:45:53 +0400 |
commit | 1e64333c5fe81a36373f0d437e425e8cab107a91 (patch) | |
tree | 1c22053446399cd513c420a30fe5365244dda305 /app/contexts | |
parent | 1a2e11c891ef4a2a87b84f2b0b36316bb809ff14 (diff) |
Refactor FilterContext
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/contexts')
-rw-r--r-- | app/contexts/filter_context.rb | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/app/contexts/filter_context.rb b/app/contexts/filter_context.rb index 2607b12b4ae..fb0d42cf87d 100644 --- a/app/contexts/filter_context.rb +++ b/app/contexts/filter_context.rb @@ -1,24 +1,35 @@ class FilterContext - attr_accessor :items, :params + attr_accessor :klass, :current_user, :params - def initialize(items, params) - @items = items + def initialize(klass, current_user, params) + @klass = klass + @current_user = current_user @params = params end def execute - apply_filter(items) + items = by_scope + items = by_state(items) + items = by_project(items) + items = by_search(items) end - def apply_filter items - if params[:project_id].present? - items = items.of_projects(params[:project_id]) - end + private - if params[:search].present? - items = items.search(params[:search]) + def by_scope + table_name = klass.table_name + + case params[:scope] + when 'authored' then + current_user.send(table_name) + when 'all' then + klass.of_projects(current_user.authorized_projects.pluck(:id)) + else + current_user.send("assigned_#{table_name}") end + end + def by_state(items) case params[:status] when 'closed' items.closed @@ -28,4 +39,20 @@ class FilterContext items.opened end end + + def by_project(items) + if params[:project_id].present? + items = items.of_projects(params[:project_id]) + end + + items + end + + def by_search(items) + if params[:search].present? + items = items.search(params[:search]) + end + + items + end end |