assigns_has_many_through_relations 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9051aa8468b71a40fe4182504535bb1a88c3353b
4
- data.tar.gz: 44885213a703333b2b7a31383d1e028f86e2ee6d
3
+ metadata.gz: 308f5f480e2b83971454b1f22f1117aa9b704ba2
4
+ data.tar.gz: 87e6562f11300bcea323e389237ceed5c8911a91
5
5
  SHA512:
6
- metadata.gz: a8958ba48851003d24e46c4005d8805d3c9ed3756f61fd1990a71a6e17c92d5b8f6be648a561c408199acd61f0a1895f427e5df244b76d03d06aac7b65d848f2
7
- data.tar.gz: 31fe8c803af72c0b924360d4ffb20ce80390db5d6c7bfc18467b074c2de444662fdd5c1300b701cef36f063fe81975612b04a4e6f9ecd8c5645ce72d5bd5413c
6
+ metadata.gz: 2a7375183004ea1e720ce949abe702219b362ea53d99132a02dad725c3f1de5a1117196303f9e6055cf03e60647422b04a87c2888647b0606fd07ce02476e36d
7
+ data.tar.gz: 6d2a10591c8580f78d43839e6ba9eaf087e19a711a49f59abeb2d883eb4b26cde2282d4aba4f4c73be5aea53269a30c4f38d9cc38041a53cb94390a05ae883f8
data/README.md CHANGED
@@ -77,19 +77,54 @@ Finally, render the management UI partial in a view template in `app/views/locat
77
77
 
78
78
  You'll have to provide the user with a link to `locations_users_path`. And that's it. Now you'll be able to assign and unassign `User`s to `Location`s.
79
79
 
80
- You can configure the engine to run a controller authorization method as you would a controller macro e.g like [Cancan's](https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-Controller-Actions) `authorize_resource`:
80
+ **Note:** _currently the left side model has to respond_to `name` for a nice display name, and the right side model has to respond to `full_name`. This will be configurable soon._
81
+
82
+ ## Configuration
83
+
84
+ You can configure the engine in an initializer. The given examples are the defaults except for `auth_filter`. That won't run if you don't set it.
81
85
 
82
86
  ```ruby
83
87
  # config/initializers/assigns_has_many_through_relations.rb
84
88
 
85
89
  AHMTR.configure do
90
+ # A controller authorization method to call as you would a controller macro.
91
+ #
92
+ # e.g like Cancan's authorize_resource (https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-Controller-Actions)
86
93
  auth_filter :authorize_resource
94
+
95
+ # The scope that loads the left side models.
96
+ #
97
+ # e.g. in the case where you declare assigns_has_many_relationships_with :location, :user
98
+ # then this scope would essentially load:
99
+ # Location.all
100
+ left_relation_scope do |left_relation_class, current_user|
101
+ left_relation_class.all
102
+ end
103
+
104
+ # The scope that loads the selected left side model's right relations.
105
+ #
106
+ # e.g. in the case where you declare assigns_has_many_relationships_with :location, :user
107
+ # then this scope would essentially load:
108
+ # @location.users
109
+ selected_right_relation_scope do |left_side_model, right_relation_class, current_user|
110
+ left_side_model.users
111
+ end
112
+
113
+ # The scope that loads all the non selected left side model's right relations.
114
+ #
115
+ # e.g. in the case where you declare assigns_has_many_relationships_with :location, :user
116
+ # then this scope would essentially load:
117
+ # User.all - @location.users
118
+ available_right_relation_scope do |right_relation_class, right_models, current_user|
119
+ right_relation_class.all - right_models
120
+ end
87
121
  end
88
122
  ```
89
123
 
90
124
  ## Todo
91
125
 
92
126
  1. Write specs.
127
+ 2. Configurable model name method.
93
128
 
94
129
  ## Contributing
95
130
 
@@ -13,7 +13,7 @@ module AssignsHasManyThroughRelations
13
13
 
14
14
  class << self
15
15
  attr_reader :config
16
- delegate :auth_filter, to: :config
16
+ delegate *Configuration::PROPERTIES, to: :config
17
17
 
18
18
  def configure(&block)
19
19
  @config ||= Configuration.new
@@ -1,10 +1,70 @@
1
1
  module AssignsHasManyThroughRelations
2
2
  class Configuration
3
+ PROPERTIES = %w[
4
+ auth_filter
5
+ left_relation_scope
6
+ selected_right_relation_scope
7
+ available_right_relation_scope
8
+ ]
9
+
3
10
  # Assign the name of a controller class method that you want to run for authorization
4
11
  # e.g. Cancan's :load_and_authorize_resource, or a custom method you define
5
12
  # in your ApplicationController
6
13
  def auth_filter(val = nil)
7
14
  @auth_filter ||= val
8
15
  end
16
+
17
+ # Set a block that will return an ActiveRecord scope of the left side models.
18
+ # the block will receive 2 arguments when it is called in the index action:
19
+ # left_relation_class: class of the left side relation
20
+ # user: the current user
21
+ def left_relation_scope(&block)
22
+ @left_relation_scope ||= block_given? ? block : method(:default_left_relation_scope)
23
+ end
24
+
25
+ # Set a block that will return an ActiveRecord scope of the selected left side model's
26
+ # right side relations. The block will receive 2 arguments when it is called in the
27
+ # index action:
28
+ # left_side_model: the selected left side model
29
+ # right_relation_class: class of the right side relation
30
+ # user: the current user
31
+ def selected_right_relation_scope(&block)
32
+ @selected_right_relation_scope ||= block_given? ? block : method(:default_selected_right_relation_scope)
33
+ end
34
+
35
+ def available_right_relation_scope(&block)
36
+ @available_right_relation_scope ||= block_given? ? block : method(:default_available_right_relation_scope)
37
+ end
38
+
39
+ private
40
+
41
+ # The default scope just loads all of the left side models
42
+ #
43
+ # e.g. n the case where you declare assigns_has_many_relationships_with :location, :user
44
+ # then the scope would essentially load:
45
+ # Location.all
46
+ def default_left_relation_scope(left_relation_class, user)
47
+ left_relation_class.all
48
+ end
49
+
50
+ # The default selected right relation scope loads all of the selected left model's
51
+ # right side relations.
52
+ #
53
+ # e.g. in the case where you declare assigns_has_many_relationships_with :location, :user
54
+ # then this scope would essentially load:
55
+ # @location.users
56
+ def default_selected_right_relation_scope(left_side_model, right_relation_class, user)
57
+ left_model.send right_relation_class.name.demodulize.underscore.pluralize
58
+ end
59
+
60
+ # The default available right relation scope loads all of the right relation model
61
+ # except for the selected right models returned by selected_right_relation_scope
62
+ #
63
+ # e.g. in the case where you declare assigns_has_many_relationships_with :location, :user
64
+ # then this scope would essentially load:
65
+ # User.all - @location.users
66
+ def default_available_right_relation_scope(right_relation_class, right_models, user)
67
+ right_relation_class.all - right_models
68
+ end
9
69
  end
10
70
  end
@@ -22,11 +22,24 @@ module AssignsHasManyThroughRelations
22
22
 
23
23
  module ControllerInstanceMethods
24
24
  def index
25
- @left_side_models = self.class.left_relation_class.order :name
25
+ @left_side_models = AHMTR.left_relation_scope.call(
26
+ self.class.left_relation_class,
27
+ current_user
28
+ )
26
29
  @selected_left_side_model = self.class.left_relation_class.find params[:id]
27
30
  @left_side_models = @left_side_models - [@selected_left_side_model]
28
- @selected_right_side_models = @selected_left_side_model.users
29
- @available_right_side_models = User.active - @selected_right_side_models
31
+
32
+ @selected_right_side_models = AHMTR.selected_right_relation_scope.call(
33
+ @selected_left_side_model,
34
+ self.class.right_relation_class,
35
+ current_user
36
+ )
37
+
38
+ @available_right_side_models = AHMTR.available_right_relation_scope.call(
39
+ self.class.right_relation_class,
40
+ @selected_right_side_models,
41
+ current_user
42
+ )
30
43
  end
31
44
 
32
45
  def update
@@ -1,3 +1,3 @@
1
1
  module AssignsHasManyThroughRelations
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assigns_has_many_through_relations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Salazar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails