DeDoDrop 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: 5a3e5831be21c6b2baedd036eed6f6484ecc1bed
4
+ data.tar.gz: ecac2400377534ef29b280f9672293ff1d31705c
5
+ SHA512:
6
+ metadata.gz: 271bb2c6fd37d4573ef277537e298b9c6d2adf0582bc490cc6ff97081933b6b2f02b8f47b5760a2f4d5c15b7b4774bdb494d7bcd91529dce257a21546999a740
7
+ data.tar.gz: 227765bf5fd0b303327c442de41db5f27dfec160cf19728baf8f074dfd01a54aa5e69b4f1cc8b15133fbb1192efd66852064784d097c05268fd54b19447ff3d0
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate dedodrop Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,53 @@
1
+ require 'rails/generators/base'
2
+ class DeDoDropGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def execute
6
+ copy_partial
7
+ copy_css
8
+ copy_js
9
+ copy_rb
10
+ end
11
+
12
+ private
13
+
14
+ def copy_partial
15
+ p "Creating 'shared' folder in views..."
16
+ if system "mkdir app/views/shared"
17
+ "Folder created."
18
+ else
19
+ "Folder already existed."
20
+ end
21
+ copy_file "_dedodrop.html.erb", "app/views/shared/_dedodrop.html.erb"
22
+ end
23
+
24
+ def copy_css
25
+ p "Creating 'dedodrop.css' folder in assets/stylesheets..."
26
+ if system "mkdir app/assets/stylesheets"
27
+ "Folder created."
28
+ else
29
+ "Folder already existed."
30
+ end
31
+ copy_file "dedodrop.css", "app/assets/stylesheets/dedodrop.css"
32
+ end
33
+
34
+ def copy_js
35
+ p "Creating 'dedodrop.js' folder in assets/javascripts..."
36
+ if system "mkdir app/assets/javascripts"
37
+ "Folder created."
38
+ else
39
+ "Folder already existed."
40
+ end
41
+ copy_file "dedodrop.js", "app/assets/javascripts/dedodrop.js"
42
+ end
43
+
44
+ def copy_rb
45
+ p "Creating 'de_do_drop.rb' folder in lib/assets/de_do_drop..."
46
+ if system "mkdir lib/assets/de_do_drop"
47
+ "Folder created."
48
+ else
49
+ "Folder already existed."
50
+ end
51
+ copy_file "de_do_drop.rb", "lib/assets/de_do_drop/de_do_drop.rb"
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ <%=content_tag(:span, "#{@menu[:name]}",style: "display:none", id:"dedodrop-primary")%>
2
+ <%=content_tag(:span, "#{@menu[:has_many]}",style: "display:none", id:"dedodrop-secondary")%>
3
+ <%= select_tag @menu[:name], options_for_select(@menu[:parents]) %><br>
4
+ <%= select_tag @menu[:has_many], options_for_select(@menu[:children]), disabled: true %><br>
@@ -0,0 +1,46 @@
1
+ class DeDoDrop
2
+ def initialize(params)
3
+ name = params.fetch(:name,params.fetch(:model)).downcase
4
+ child = params.fetch(:has_many).downcase.chop
5
+ @all_models = eval params.fetch(:model)+".all"
6
+ @menu = {name: name,
7
+ has_many: params.fetch(:has_many).downcase,
8
+ parents: [["Select #{name}", nil]],
9
+ children: [["Select #{name} first", nil, {id:"dedodrop-kill"}],["Select option","-"]]}
10
+ @menu[:parents] += get_parents(params)
11
+ @menu[:children] += get_children(params)
12
+ end
13
+
14
+ def menu
15
+ @menu
16
+ end
17
+
18
+ private
19
+
20
+ def get_parents(params)
21
+ model_attr_st = "model.#{params.fetch(:model_attr)}"
22
+ @all_models.map do |model|
23
+ model_attr = eval model_attr_st
24
+ [model_attr, model.id, {class: "hi"}]
25
+ end
26
+ end
27
+
28
+ def get_children(params)
29
+ has_many = params.fetch(:has_many)
30
+ children_st = "model.#{has_many}"
31
+ child_attr_st ="child.#{params.fetch(:child_attr)}"
32
+ children = []
33
+ @all_models.map do |model|
34
+ model_children = eval children_st
35
+ children += model_children.map do |child|
36
+ child_attr = eval child_attr_st
37
+ [child_attr, child.id, {class: ["dedodrop-hidden", "dedodrop-id#{model.id}"]}]
38
+ end
39
+ end
40
+ children
41
+ end
42
+
43
+ def self.procfy(string)
44
+ string = "proc{#{string}}"
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ .dedodrop-hidden {
2
+ display: none !important;
3
+ }
4
+
5
+ .dedodrop-visible {
6
+ display: inline !important;
7
+ }
@@ -0,0 +1,58 @@
1
+ // You might have to remove this code if you have other JS initializers.
2
+ // Copy the initialize function in your JS initializer
3
+
4
+ window.addEventListener( 'load', initialize)
5
+
6
+ function initialize(){
7
+ deDoDrop = new DeDropDo()
8
+ deDoDrop.bindEventListeners()
9
+ }
10
+
11
+ // END
12
+
13
+
14
+
15
+ function DeDropDo() {
16
+ this.primary = document.getElementById("dedodrop-primary").innerHTML
17
+ this.secondary = document.getElementById("dedodrop-secondary").innerHTML
18
+ this.old = ""
19
+ }
20
+
21
+ DeDropDo.prototype = {
22
+ bindEventListeners: function(){
23
+ var primaryList = document.getElementById(deDoDrop.primary)
24
+ primaryList.addEventListener("change", this.actions, false);
25
+ },
26
+ actions: function(){
27
+ deDoDrop.hideOld(deDoDrop.old)
28
+ var id = "dedodrop-id"+this.value;
29
+ deDoDrop.showOptions(id)
30
+ deDoDrop.killFirst()
31
+ deDoDrop.enableSecondary()
32
+ },
33
+ enableSecondary: function(){
34
+ var childrenList = document.getElementById(deDoDrop.secondary);
35
+ childrenList.disabled = false;
36
+ },
37
+ hideOld: function(id){
38
+ var options = document.getElementsByClassName(id);
39
+ var l = options.length;
40
+ for (var i = 0; i<l; i++){
41
+ options[i].className = id + " dedodrop-hidden"
42
+ }
43
+ },
44
+ showOptions: function(id){
45
+ deDoDrop.old = id
46
+ var options = document.getElementsByClassName(id);
47
+ var l = options.length;
48
+ for (var i = 0; i<l; i++){
49
+ options[i].className = id + " dedodrop-visible"
50
+ }
51
+ },
52
+ killFirst: function(){
53
+ victim = document.getElementById("dedodrop-kill")
54
+ if (victim !== null)
55
+ victim.remove()
56
+
57
+ }
58
+ }
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DeDoDrop
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alejandro E. Losada
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: DeDoDrop is a gem to hande dependent drop downs. It can only handle one
14
+ dependency at the moment.
15
+ email: alejandrolosada@alejandrolosada.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/generators/de_do_drop/de_do_drop_generator.rb
21
+ - lib/generators/de_do_drop/templates/_dedodrop.html.erb
22
+ - lib/generators/de_do_drop/templates/de_do_drop.rb
23
+ - lib/generators/de_do_drop/templates/dedodrop.css
24
+ - lib/generators/de_do_drop/templates/dedodrop.js
25
+ - lib/generators/de_do_drop/USAGE
26
+ homepage: http://rubygems.org/gems/de_do_drop
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.14
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: DeDoDrop is a gem to hande dependent drop downs.
50
+ test_files: []