assigns_has_many_through_relations 0.0.1

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: 091ce286a1c1e886f54fa1afaed43e33148ce45e
4
+ data.tar.gz: 9a97881cf7f3b91b55de3be7ce3378403518b319
5
+ SHA512:
6
+ metadata.gz: 1c403f598488a5b190100f58bb6d1607b89fcd458ccefbb5b61f12ab03accc549224b7dc9e4f4db45bd507b528440a56033bda4abebac421fdcafe10f068869a
7
+ data.tar.gz: 4a072185ea985fc2bb3933b4a94de76de1395ad85b87c9a908129605574805d6ab1ea5ee48a8698fa63863c9dea7c257e58fe7986109d98c9455c913c5ebfb02
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in assigns_has_many_through_relations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Diego Salazar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # AssignsHasManyThroughRelations
2
+
3
+ Rails Engine that provides a management UI to assign models to each other in the case where they are associated in a `has_many` `:through` style relationship.
4
+
5
+ For example, given a pair of models `Location` and `User`, we will consider `Location` to be the left side relation and `User` to be the right side relation. This UI provides a 3 column Bootstrap view where the left most column displays all `Location`s where the user can select one from the list. The 2 other columns are the Assigned and Unnassigned columns. These display `User`s that either have a join model with the selected `Location` or not, respectively. The user can then select `User`s from the Unnassigned column and the form PUT will create the join models necessary to move the Unnassigned `User`s to the middle Assigned column, effectively associating the selected `User`s from the right most column to the selected `Location`.
6
+
7
+ ## Example UI
8
+ ![assigns_has_many_through_relations_screenshot](https://cloud.githubusercontent.com/assets/89930/6175967/0d86cf9e-b2c9-11e4-85d8-79c58d8570d6.png)
9
+
10
+ In the above example, clicking on "Assign Selected" will move the selected `User`s to the middle column by creating the `locations_user` join model between them and the selected `Location` "Home".
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'assigns_has_many_through_relations'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install assigns_has_many_through_relations
27
+
28
+ ## Usage
29
+
30
+ Declare the routes:
31
+
32
+ ```ruby
33
+ YourApp::Application.routes.draw do
34
+ AssignsHasManyThroughRelations.routes_for :locations, :users, self
35
+ end
36
+ ```
37
+
38
+ Declare the plugin and options in your models:
39
+
40
+ ```ruby
41
+ class Location < ActiveRecord::Base
42
+ extend AssignsHasManyThroughRelations::ModelConcern
43
+ assigns_has_many_relationships_with :location, :user
44
+ end
45
+
46
+ class User < ActiveRecord::Base
47
+ has_many :locations_users, dependent: :delete_all
48
+ has_many :locations, through: :locations_users
49
+ end
50
+ ```
51
+
52
+ And a controller to handle the requests:
53
+
54
+ ```ruby
55
+ class LocationsUsersController < ApplicationController
56
+ extend AssignsHasManyThroughRelations::ControllerConcern
57
+ assigns_has_many_relationships_with :location, :user
58
+ end
59
+ ```
60
+
61
+ Finally, render the management UI partial in a view template in `app/views/locations_users/index.html.erb`:
62
+
63
+ ```html
64
+ <div id="locations_users">
65
+ <h1>Assign Users To Location</h1>
66
+
67
+ <%= render '/assigns_has_many_through_relations' %>
68
+ </div>
69
+ ```
70
+
71
+ 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.
72
+
73
+ ## Todo
74
+
75
+ 1. Move the quick_list_filter.js file into the engine.
76
+ 2. Write specs.
77
+ 3. Clean up css classes and ids in the partial.
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/assigns_has_many_through_relations/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
Binary file
@@ -0,0 +1,113 @@
1
+ <div class="flash-messages row">
2
+ <div class="col-sm-12">
3
+ <% flash.each do |key, message| %>
4
+ <div class="alert alert-<%= AssignsHasManyThroughRelations::BOOTSTRAP_FLASH_MAP[key] %>" role="alert">
5
+ <%= message %>
6
+ </div>
7
+ <% end %>
8
+ </div>
9
+ </div>
10
+
11
+ <div id="roles" class="row">
12
+ <div class="col-sm-3">
13
+ <div class="panel panel-info">
14
+ <div class="panel-heading">
15
+ <%= pluralize @left_side_models.size + 1, left_relation_class_name.pluralize %>
16
+ </div>
17
+
18
+ <div id="role-nav-panel" class="panel-body">
19
+ <%= render 'shared/quick_list_filter', target: '#roles-nav' %>
20
+
21
+ <ul id="roles-nav" class="nav nav-stacked fix-height">
22
+ <li role="presentation" class="active">
23
+ <%= link_to send("#{join_name}_path", @selected_left_side_model) do %>
24
+ <%= @selected_left_side_model.name %>
25
+ <span class="glyphicon glyphicon-chevron-right pull-right"></span>
26
+ <% end %>
27
+ </li>
28
+
29
+ <% @left_side_models.each do |left_side_model| %>
30
+ <li role="presentation">
31
+ <%= link_to left_side_model.name, send("#{join_name}_path", left_side_model) %>
32
+ </li>
33
+ <% end %>
34
+ </ul>
35
+
36
+ <div class="text-center hide-when-no-scrollbars" data-target="#roles-nav">
37
+ <span class="glyphicon glyphicon-chevron-down disabled"></span>
38
+ </div>
39
+ </div>
40
+ </div>
41
+ </div>
42
+
43
+ <%= form_for @selected_left_side_model, url: send("assign_#{join_name}_path", @selected_left_side_model) do |f| %>
44
+ <div class="col-sm-9">
45
+ <div class="row">
46
+ <div class="col-sm-6">
47
+ <div class="panel panel-primary roles-users-list pull-left">
48
+ <div class="panel-heading">
49
+ <%= pluralize @selected_right_side_models.size, "#{@selected_left_side_model.name} User" %>
50
+ </div>
51
+
52
+ <div class="panel-body">
53
+ <%= render 'shared/quick_list_filter', target: '#selected-users' %>
54
+
55
+ <ul id="selected-users" class="list-unstyled fix-height">
56
+ <% @selected_right_side_models.each do |model| %>
57
+ <li>
58
+ <%= f.fields_for join_name, @selected_left_side_model.get_join_model_for(model) do |ruf| %>
59
+ <%= ruf.hidden_field :id, class: 'pull-left' %>
60
+ <%= ruf.check_box :_destroy, class: 'pull-left' %>
61
+ <%= ruf.label :_destroy, model.full_name, class: 'pull-left' %>
62
+ <% end %>
63
+ </li>
64
+ <% end %>
65
+ </ul>
66
+
67
+ <div class="text-center hide-when-no-scrollbars" data-target="#selected-users">
68
+ <span class="glyphicon glyphicon-chevron-down disabled"></span>
69
+ </div>
70
+ </div>
71
+
72
+ <div class="panel-footer">
73
+ <%= f.submit 'Unassign Selected', class: 'btn btn-warning btn-sm' %>
74
+ <%= f.submit 'Unassign All', class: 'btn btn-danger btn-sm check-all-boxes', data: { target: '#selected-users :checkbox' } %>
75
+ </div>
76
+ </div>
77
+ </div>
78
+
79
+ <div class="col-sm-6">
80
+ <div class="panel panel-danger roles-users-list pull-left">
81
+ <div class="panel-heading">
82
+ <%= pluralize @available_right_side_models.size, "Non #{@selected_left_side_model.name} User" %>
83
+ </div>
84
+
85
+ <div class="panel-body">
86
+ <%= render 'shared/quick_list_filter', target: '#available-users' %>
87
+
88
+ <ul id="available-users" class="list-unstyled fix-height">
89
+ <% @available_right_side_models.each do |model| %>
90
+ <li>
91
+ <%= f.fields_for join_name, model.send(join_name).build do |ruf| %>
92
+ <%= ruf.check_box "#{model.class.name.demodulize.underscore}_id", { checked: false }, model.id, nil %>
93
+ <%= ruf.label "#{model.class.name.demodulize.underscore}_id", model.full_name %>
94
+ <% end %>
95
+ </li>
96
+ <% end %>
97
+ </ul>
98
+
99
+ <div class="text-center hide-when-no-scrollbars" data-target="#available-users">
100
+ <span class="glyphicon glyphicon-chevron-down disabled"></span>
101
+ </div>
102
+ </div>
103
+
104
+ <div class="panel-footer">
105
+ <%= f.submit 'Assign Selected', class: 'btn btn-success btn-sm' %>
106
+ <%= f.submit 'Assign All', class: 'btn btn-primary btn-sm check-all-boxes', data: { target: '#available-users :checkbox' } %>
107
+ </div>
108
+ </div>
109
+ </div>
110
+ </div>
111
+ </div>
112
+ <% end %>
113
+ </div>
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'assigns_has_many_through_relations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "assigns_has_many_through_relations"
8
+ spec.version = AssignsHasManyThroughRelations::VERSION
9
+ spec.authors = ["Diego Salazar"]
10
+ spec.email = ["diego@greyrobot.com"]
11
+ spec.summary = %q{Rails Engine that provides a management UI to assign models to each other}
12
+ spec.description = %q{Rails Engine that provides a management UI to assign models to each other}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'rails'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,28 @@
1
+ require "assigns_has_many_through_relations/version"
2
+ require "assigns_has_many_through_relations/engine"
3
+
4
+ module AssignsHasManyThroughRelations
5
+ BOOTSTRAP_FLASH_MAP = {
6
+ notice: 'success',
7
+ error: 'danger'
8
+ }
9
+ BOOTSTRAP_FLASH_MAP.default = 'info'
10
+
11
+ autoload :ControllerConcern, 'assigns_has_many_through_relations/controller_concern'
12
+ autoload :ModelConcern, 'assigns_has_many_through_relations/model_concern'
13
+
14
+ module_function
15
+
16
+ def join_name_for(left_relation, right_relation)
17
+ "#{left_relation.to_s.pluralize}_#{right_relation.to_s.pluralize}"
18
+ end
19
+
20
+ def routes_for(left_relation, right_relation, routes)
21
+ name = join_name_for left_relation, right_relation
22
+ left_name = left_relation.to_s.pluralize
23
+ right_name = right_relation.to_s.pluralize
24
+
25
+ routes.get "/#{left_name}/#{right_name}/:id", to: "#{name}#index", as: name
26
+ routes.put "/#{left_name}/#{right_name}/:id", to: "#{name}#update", as: "assign_#{name}"
27
+ end
28
+ end
@@ -0,0 +1,47 @@
1
+ module AssignsHasManyThroughRelations
2
+ module ControllerConcern
3
+ def assigns_has_many_relationships_with(left_relation, right_relation)
4
+ @left_relation_param_name = left_relation
5
+ @left_relation_class = left_relation.to_s.camelize.constantize
6
+ @left_relation_class_name = @left_relation_class.name
7
+ @right_relation_class = right_relation.to_s.camelize.constantize
8
+ @join_name = AssignsHasManyThroughRelations.join_name_for left_relation, right_relation
9
+
10
+ include AssignsHasManyThroughRelations::ControllerInstanceMethods
11
+ helper_method :left_relation_class_name, :join_name
12
+ end
13
+
14
+ def left_relation_param_name; @left_relation_param_name end
15
+ def left_relation_class; @left_relation_class end
16
+ def left_relation_class_name; @left_relation_class_name end
17
+ def right_relation_class; @right_relation_class end
18
+ def join_name; @join_name end
19
+ end
20
+
21
+ module ControllerInstanceMethods
22
+ def index
23
+ @left_side_models = self.class.left_relation_class.order :name
24
+ @selected_left_side_model = self.class.left_relation_class.find params[:id]
25
+ @left_side_models = @left_side_models - [@selected_left_side_model]
26
+ @selected_right_side_models = @selected_left_side_model.users
27
+ @available_right_side_models = User.active - @selected_right_side_models
28
+ end
29
+
30
+ def update
31
+ left_side_model = self.class.left_relation_class.find params[:id]
32
+
33
+ if left_side_model.update_attributes params[self.class.left_relation_param_name]
34
+ flash[:notice] = "Successfully set #{self.class.left_relation_param_name} assignments"
35
+ else
36
+ flash[:error] = left_side_model.errors.full_messages.to_sentence
37
+ end
38
+
39
+ redirect_to :back
40
+ end
41
+
42
+ private
43
+
44
+ def left_relation_class_name; self.class.left_relation_class_name end
45
+ def join_name; self.class.join_name end
46
+ end
47
+ end
@@ -0,0 +1,4 @@
1
+ module AssignsHasManyThroughRelations
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ module AssignsHasManyThroughRelations
2
+ module ModelConcern
3
+ def assigns_has_many_relationships_with(left_relation, right_relation)
4
+ @join_name = AssignsHasManyThroughRelations.join_name_for left_relation, right_relation
5
+
6
+ has_many @join_name.to_sym, dependent: :delete_all
7
+ has_many right_relation.to_s.pluralize.to_sym, through: @join_name.to_sym
8
+ accepts_nested_attributes_for @join_name.to_sym, allow_destroy: true
9
+ attr_accessible "#{@join_name}_attributes"
10
+
11
+ include AssignsHasManyThroughRelations::ModelInstanceMethods
12
+ end
13
+
14
+ def join_name; @join_name end
15
+ end
16
+
17
+ module ModelInstanceMethods
18
+ def get_join_model_for(relation)
19
+ send(self.class.join_name).where("#{relation.class.name.underscore}_id" => relation.id).first
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module AssignsHasManyThroughRelations
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assigns_has_many_through_relations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diego Salazar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
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: Rails Engine that provides a management UI to assign models to each other
56
+ email:
57
+ - diego@greyrobot.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - app/views/.DS_Store
68
+ - app/views/_assigns_has_many_through_relations.html.erb
69
+ - assigns_has_many_through_relations.gemspec
70
+ - lib/assigns_has_many_through_relations.rb
71
+ - lib/assigns_has_many_through_relations/controller_concern.rb
72
+ - lib/assigns_has_many_through_relations/engine.rb
73
+ - lib/assigns_has_many_through_relations/model_concern.rb
74
+ - lib/assigns_has_many_through_relations/version.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Rails Engine that provides a management UI to assign models to each other
99
+ test_files: []