backbone-chooser-rails 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5747456fd47f6847c485230f5aba35a37edcb11
4
+ data.tar.gz: ac389af439b640eec0a9cf856be7a0114f242a37
5
+ SHA512:
6
+ metadata.gz: 89f37e1bb8cf8f9a360df97019c6866534b7b037c70503e3f56ebf73f397f51ec1f7c3e7b6eb1e89b06acb2f7f9e3e37c58246b637d13e3c811c6ea13bf82e98
7
+ data.tar.gz: 186fa0f6a0057d307a78afc9ae4e02626335ea92672353d2e8c61bc9cfbe629f5be943a798d3ca4a0f691d52bc234ab03b77d51dabf132ba4aea28d1c0b43d22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yevhen V.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Backbone.Chooser-Rails
2
+
3
+ backbone-chooser-rails wraps the [Backbone.Chooser](https://github.com/brian-mann/Backbone.Chooser)
4
+ plugin in a rails engine for simple use with the asset pipeline provided by
5
+ Rails 3.1 and higher. The gem includes the development (non-minified) source
6
+ for ease of exploration. The asset pipeline will minify in production.
7
+
8
+ Backbone.Chooser plugin allows to easily track your selected, aka chosen models
9
+ and collection.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'backbone-chooser-rails'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install backbone-chooser-rails
26
+
27
+ ## Usage
28
+
29
+ Add the following directive to your Javascript manifest file (application.js):
30
+
31
+ //= require backbone-chooser
32
+
33
+ For the further details, please refer to the [Backbone.Chooser](https://github.com/brian-mann/Backbone.Chooser)
34
+ documentation.
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yevgenko/backbone-chooser-rails.
45
+
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
50
+
@@ -0,0 +1,10 @@
1
+ require "backbone/chooser/rails/version"
2
+
3
+ module Backbone
4
+ module Chooser
5
+ module Rails
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module Backbone
2
+ module Chooser
3
+ module Rails
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,178 @@
1
+ do (Backbone) ->
2
+
3
+ class Backbone.Chooser
4
+ constructor: (model) ->
5
+ @model = model
6
+ @model._chooser = @
7
+
8
+ @model[method] = _.bind(@[method], @) for method in @_publicMethods()
9
+
10
+ @chosen = false
11
+ @model.set chosen: false
12
+
13
+ _publicMethods: -> ["choose", "unchoose", "toggleChoose", "isChosen"]
14
+
15
+ isChosen: ->
16
+ !!@chosen
17
+
18
+ choose: (options = {}) ->
19
+ return if @isChosen()
20
+
21
+ @chosen = true
22
+ @model.set chosen: true, options
23
+ @model.trigger "model:chosen", @model unless options.silent is true
24
+ @model.collection?.choose?(@model, options)
25
+
26
+ unchoose: (options = {}) ->
27
+ return if !@isChosen()
28
+
29
+ @chosen = false
30
+ @model.set chosen: false, options
31
+ @model.trigger "model:unchosen", @model unless options.silent is true
32
+ @model.collection?.unchoose?(@model, options)
33
+
34
+ toggleChoose: ->
35
+ if @isChosen() then @unchoose() else @choose()
36
+
37
+ class BaseChooser
38
+ constructor: (collection) ->
39
+ @collection = collection
40
+
41
+ @collection._chooser = @
42
+ @collection._chooser.chosen = {}
43
+
44
+ @collection[method] = _.bind(@[method], @) for method in @_publicMethods()
45
+
46
+ _publicMethods: -> ["choose", "unchoose", "getChosen", "getFirstChosen", "chooseById"]
47
+
48
+ getChosen: ->
49
+ _.toArray @chosen
50
+
51
+ getFirstChosen: ->
52
+ @getChosen()[0]
53
+
54
+ modelInChosen: (model) ->
55
+ model.cid in _.keys @chosen
56
+
57
+ addModel: (model, options = {}) ->
58
+ @chosen[model.cid] = model
59
+ model.choose?(options)
60
+
61
+ removeModels: (model = false) ->
62
+ for model in _.flatten [model or @getChosen()]
63
+ delete @chosen[model.cid]
64
+ model.unchoose?()
65
+
66
+ triggerEvent: (event = false, options = {}) ->
67
+ _.defaults options, silent: false
68
+
69
+ return if options.silent is true
70
+
71
+ event or= @_getEvent()
72
+ @collection.trigger event, @_eventArg()
73
+
74
+ chooseById: (id, options = {}) ->
75
+ model = @collection.get(id)
76
+ @choose model, options if model
77
+
78
+ class Backbone.SingleChooser extends BaseChooser
79
+ ## return only the first model as the event argument
80
+ _eventArg: -> @getFirstChosen()
81
+
82
+ choose: (model, options) ->
83
+ ## return if the model is already chosen
84
+ return if @modelInChosen(model)
85
+
86
+ @removeModels()
87
+
88
+ ## add the model to the chosen array
89
+ @addModel(model)
90
+
91
+ ## fire a chosen event
92
+ @triggerEvent "collection:chose:one", options
93
+
94
+ unchoose: (model, options) ->
95
+ return if !@modelInChosen(model)
96
+
97
+ @removeModels(model)
98
+
99
+ ## fire a unchose event
100
+ @triggerEvent "collection:unchose:one", options
101
+
102
+ class Backbone.MultiChooser extends BaseChooser
103
+ constructor: ->
104
+ super
105
+ @collection[method] = _.bind(@[method], @) for method in ["chooseAll", "chooseNone", "chooseByIds"]
106
+
107
+ ## return all of the chosen models as the event argument
108
+ _eventArg: -> @getChosen()
109
+
110
+ choose: (args...) ->
111
+ options = if _.chain(args).flatten().last().value() not instanceof Backbone.Model then args.pop() else {}
112
+
113
+ eventShouldTrigger = false
114
+
115
+ for model in _([args]).flatten()
116
+ ## break if the model is already chosen
117
+ break if @modelInChosen(model)
118
+
119
+ ## know to trigger a one time event
120
+ eventShouldTrigger or= true
121
+
122
+ ## add the model to the chosen array
123
+ @addModel(model, options)
124
+
125
+ ## fire event
126
+ @triggerEvent(false, options) if eventShouldTrigger
127
+
128
+ unchoose: (args...) ->
129
+ options = if _.chain(args).flatten().last().value() not instanceof Backbone.Model then args.pop() else {}
130
+
131
+ eventShouldTrigger = false
132
+
133
+ for model in _([args]).flatten()
134
+ ## break if the model is already chosen
135
+ break if !@modelInChosen(model)
136
+
137
+ ## know to trigger a one time event
138
+ eventShouldTrigger or= true
139
+
140
+ ## add the model to the chosen array
141
+ @removeModels(model, options)
142
+
143
+ ## fire event
144
+ @triggerEvent(false, options) if eventShouldTrigger
145
+
146
+ chooseAll: (options = {}) ->
147
+ return if not _.difference(@collection.models, @getChosen()).length
148
+
149
+ @addModel(model) for model in @collection.models
150
+
151
+ @triggerEvent(false, options)
152
+
153
+ chooseNone: (options = {}) ->
154
+ return if @getChosen().length is 0
155
+
156
+ @removeModels()
157
+
158
+ @triggerEvent(false, options)
159
+
160
+ ## chooses models by an array of ids
161
+ ## passing chooseNone as options will
162
+ ## first clear the current chosen
163
+ chooseByIds: (ids = [], options = {}) ->
164
+ _.defaults options, chooseNone: true
165
+
166
+ @chooseNone options if options.chooseNone
167
+
168
+ for id in _([ids]).flatten()
169
+ @chooseById id, options
170
+
171
+ _getEvent: ->
172
+ if @collection.length is @getChosen().length
173
+ return "collection:chose:all"
174
+
175
+ if @getChosen().length is 0
176
+ return "collection:chose:none"
177
+
178
+ return "collection:chose:some"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backbone-chooser-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yevhen V.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Backbone.Chooser plugin allow to easily track your selected, aka chosen
56
+ models and collection.
57
+ email:
58
+ - wik@bdhr.co
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - LICENSE.txt
64
+ - README.md
65
+ - lib/backbone/chooser/rails.rb
66
+ - lib/backbone/chooser/rails/version.rb
67
+ - vendor/assets/javascripts/backbone-chooser.coffee
68
+ homepage: https://github.com/yevgenko/backbone-chooser-rails
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.4.5
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Backbone.Chooser plugin ready to play with Rails.
92
+ test_files: []