lolita-menu 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/History.rdoc +4 -0
  2. data/README.rdoc +7 -2
  3. data/VERSION +1 -1
  4. data/app/controllers/menu_items_controller.rb +5 -0
  5. data/app/views/components/lolita/menu/_list.html.haml +2 -1
  6. data/app/views/components/lolita/menu_items/_row.html.haml +6 -1
  7. data/config/routes.rb +1 -0
  8. data/lib/generators/lolita_menu/install_generator.rb +23 -4
  9. data/lib/generators/lolita_menu/templates/lolita-menu-urls.rb +5 -0
  10. data/lib/lolita-menu/autocomplete/collector.rb +35 -0
  11. data/lib/lolita-menu/autocomplete/file_builder.rb +33 -0
  12. data/lib/lolita-menu/autocomplete/link_set.rb +32 -0
  13. data/lib/lolita-menu/{rails.rb → engine.rb} +0 -0
  14. data/lib/lolita-menu/railtie.rb +11 -0
  15. data/lib/lolita-menu.rb +21 -1
  16. data/lib/tasks/lolita-menu.rake +10 -0
  17. data/lolita-menu.gemspec +29 -7
  18. data/public/javascripts/autocomplete-rails.js +113 -0
  19. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  20. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_0_eeeeee_40x100.png +0 -0
  21. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_55_c0402a_40x100.png +0 -0
  22. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_55_eeeeee_40x100.png +0 -0
  23. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_100_f8f8f8_1x400.png +0 -0
  24. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_35_dddddd_1x400.png +0 -0
  25. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_60_eeeeee_1x400.png +0 -0
  26. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_inset-hard_75_999999_1x100.png +0 -0
  27. data/public/stylesheets/jquery-ui/overcast/images/ui-bg_inset-soft_50_c9c9c9_1x100.png +0 -0
  28. data/public/stylesheets/jquery-ui/overcast/images/ui-icons_3383bb_256x240.png +0 -0
  29. data/public/stylesheets/jquery-ui/overcast/images/ui-icons_454545_256x240.png +0 -0
  30. data/public/stylesheets/jquery-ui/overcast/images/ui-icons_70b2e1_256x240.png +0 -0
  31. data/public/stylesheets/jquery-ui/overcast/images/ui-icons_999999_256x240.png +0 -0
  32. data/public/stylesheets/jquery-ui/overcast/images/ui-icons_fbc856_256x240.png +0 -0
  33. data/public/stylesheets/jquery-ui/overcast/jquery-ui-1.8.16.custom.css +568 -0
  34. metadata +47 -27
data/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ === Version 0.0.12 / 2011-08-23
2
+ * Enhancements
3
+ * Autocomplete for url added
4
+
1
5
  === Version 0.0.9 / 2011-06-10
2
6
  * Enhancements
3
7
  * Active item detection based on request
data/README.rdoc CHANGED
@@ -9,8 +9,13 @@ And run
9
9
  Like with other Lolita gems call
10
10
  rails g lolita_menu:install
11
11
  Since this gem comes with migrations
12
- rake db:migrate
13
-
12
+ rake db:migrate
13
+ === Using autocomplete
14
+ To use autocomplete you need to add all possible url's in config/lolita-menu-urls.rb (follow instructions in file)
15
+ After that you can popuate all from file in result file by execucuting
16
+ rake lolita_menu:generate
17
+ If you want to add url when some of objects are created, then in your model add after_save or after_create callbacks and in related method add
18
+ Lolita::Menu::Urls.add("/link", "Label"), unfortuneately it's impossible to use routes helper methods unless you do your own workaround.
14
19
  Enjoy!
15
20
 
16
21
  ==Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.11
1
+ 0.0.12
@@ -3,6 +3,11 @@ class MenuItemsController < ActionController::Base
3
3
  include Lolita::Controllers::UserHelpers
4
4
 
5
5
  before_filter :authenticate_lolita_user!
6
+
7
+ def autocomplete_menu_item_url
8
+ urls = Lolita::Menu::Autocomplete::Collector.new(params[:term])
9
+ render :json => urls.to_json
10
+ end
6
11
 
7
12
  def create
8
13
  menu=Menu.find_by_id(params[:menu_id])
@@ -1,7 +1,8 @@
1
1
  - content_for :script do
2
- = javascript_include_tag "lolita/menu-items", "jquery-ui-nested-sortables"
2
+ = javascript_include_tag "lolita/menu-items", "jquery-ui-nested-sortables", "autocomplete-rails.js"
3
3
  - content_for :style do
4
4
  = stylesheet_link_tag "lolita/menu/style"
5
+ = stylesheet_link_tag 'jquery-ui/overcast/jquery-ui-1.8.16.custom'
5
6
  .box
6
7
  = render_component list, :title
7
8
  = render_component list, :new_resource
@@ -3,7 +3,12 @@
3
3
  %span
4
4
  %input{:type=>"text",:value=>item.name,:name=>"name",:"data-id"=>item.id,:"data-url"=>menu_item_path(item.id)}
5
5
  %span
6
- %input{:type=>"text", :value=>item.url,:name=>"url",:class=>"url-input",:"data-id"=>item.id,:"data-url"=>menu_item_path(item.id)}
6
+ %input{:"data-autocomplete" => autocomplete_menu_item_url_menu_items_path, :"data-delimiter" => " ",
7
+ :id => "menu_item_#{item.id}_url", :name => "url",
8
+ :class=>"url-input ui-autocomplete-input", :autocomplete=>"off", :role=>"textbox", :"aria-autocomplete"=>"list",
9
+ :"aria-haspopup"=>"true", :type => "text", :value => item.url,
10
+ :"data-id"=>item.id,:"data-url"=>menu_item_path(item.id),
11
+ }
7
12
  %span
8
13
  = image_tag("lolita-menu/move.png",:style=>"cursor:move;")
9
14
  %span
data/config/routes.rb CHANGED
@@ -3,6 +3,7 @@ Rails.application.routes.draw do
3
3
  resources :menu_items, :only=>[:create,:destroy,:update] do
4
4
  collection do
5
5
  put :update_tree
6
+ get :autocomplete_menu_item_url
6
7
  end
7
8
  end
8
9
  end
@@ -1,19 +1,38 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
1
3
  module LolitaMenu
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
4
- include Lolita::Generators::FileHelper
6
+ include Rails::Generators::Migration
7
+
5
8
  source_root File.expand_path("../templates", __FILE__)
6
9
  desc "Copy assets and create migrations. "
7
10
 
11
+ def self.next_migration_number(dirname)
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ else
15
+ "%.3d" % (current_migration_number(dirname) + 1)
16
+ end
17
+ end
8
18
 
9
19
  def copy_assets
10
20
  generate("lolita_menu:assets")
11
21
  end
12
22
 
13
- def copy_migration
14
- copy_file "migrations/create_menus.rb", "db/migrate/#{Time.now.strftime("%Y%m%d%H%M%S")}_create_lolita_menus.rb"
15
- copy_file "migrations/create_menu_items.rb", "db/migrate/#{(Time.now+1).strftime("%Y%m%d%H%M%S")}_create_lolita_menu_items.rb"
23
+ def create_menu_migrations
24
+ begin
25
+ migration_template "migrations/create_menus.rb", "db/migrate/create_lolita_menus.rb"
26
+ migration_template "migrations/create_menu_items.rb", "db/migrate/create_lolita_menu_items.rb"
27
+ rescue Exception => e
28
+ puts e
29
+ end
30
+ end
31
+
32
+ def copy_initializer
33
+ template "lolita-menu-urls.rb", "config/lolita-menu-urls.rb"
16
34
  end
35
+
17
36
  end
18
37
  end
19
38
  end
@@ -0,0 +1,5 @@
1
+ Lolita::Menu::Urls.create do
2
+ # add url and/or label to autocomplete list
3
+ # add "/contacts", "Contacts"
4
+ # add "/pages/my-contact-page"
5
+ end
@@ -0,0 +1,35 @@
1
+ module Lolita
2
+ module Menu
3
+ module Autocomplete
4
+ class Collector
5
+
6
+ def initialize(term)
7
+ @links = []
8
+ @term = term
9
+ collect_from_file
10
+ end
11
+
12
+ def collect_from_file
13
+ if File.exist?(Lolita::Menu::Autocomplete::FileBuilder.output_file)
14
+ File.open(Lolita::Menu::Autocomplete::FileBuilder.output_file, "r").readlines.each{ |line|
15
+ item = line.to_s.split(" ")
16
+ item.last.to_s.gsub!("\n")
17
+ if item.first.match(/#{@term}/) || item.last.match(/#{@term}/)
18
+ @links << item
19
+ end
20
+ }
21
+ end
22
+ end
23
+
24
+ def to_json
25
+ result = []
26
+ @links.each_with_index{|item, index|
27
+ result << {:id => index, :label => "#{item.last} #{item[1] != item[0] ? "(#{item[0]})" : ""}", :value => item.first}
28
+ }
29
+ result
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ module Lolita
2
+ module Menu
3
+ module Autocomplete
4
+ class FileBuilder
5
+
6
+ class << self
7
+ def input_file
8
+ File.join(Rails.root,"config","lolita-menu-urls.rb")
9
+ end
10
+
11
+ def output_file
12
+ File.join(Rails.root,"public","lolita-menu-url.txt")
13
+ end
14
+ end
15
+
16
+ attr_reader :file
17
+
18
+ def initialize(mode)
19
+ @file = File.open(self.class.output_file,mode)
20
+ end
21
+
22
+ def add url,label = nil
23
+ @file.puts "#{url} #{label || url}"
24
+ end
25
+
26
+ def finalize!
27
+ @file.close
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module Lolita
2
+ module Menu
3
+ module Autocomplete
4
+ class LinkSet
5
+ include ::Rails.application.routes.url_helpers
6
+
7
+ def initialize(&block)
8
+ @links = []
9
+ end
10
+
11
+ def add *args
12
+ unless @file
13
+ @file = Lolita::Menu::Autocomplete::FileBuilder.new("a")
14
+ new_stream = true
15
+ end
16
+ @file.add(*args)
17
+ @file.finalize! if new_stream
18
+ end
19
+
20
+ def create &block
21
+ @file = Lolita::Menu::Autocomplete::FileBuilder.new("w")
22
+ begin
23
+ instance_eval(&block)
24
+ ensure
25
+ @file.finalize!
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
File without changes
@@ -0,0 +1,11 @@
1
+ module Lolita
2
+ module Menu
3
+ class Railtie < Rails::Railtie
4
+ railtie_name :lolita_menu
5
+
6
+ rake_tasks do
7
+ load "tasks/lolita-menu.rake"
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/lolita-menu.rb CHANGED
@@ -5,9 +5,29 @@ require 'lolita'
5
5
 
6
6
  module Lolita
7
7
  module Menu
8
+ module Autocomplete
9
+ autoload :LinkSet, "lolita-menu/autocomplete/link_set"
10
+ autoload :FileBuilder, "lolita-menu/autocomplete/file_builder"
11
+ autoload :Collector, "lolita-menu/autocomplete/collector"
8
12
 
13
+ def self.generate
14
+ require Lolita::Menu::Autocomplete::FileBuilder.input_file
15
+ end
16
+ end
17
+ # took this from sitemap_generator
18
+ Urls = (Class.new do
19
+ def method_missing(*args, &block)
20
+ (@link_set ||= reset!).send(*args, &block)
21
+ end
22
+
23
+ # Use a new LinkSet instance
24
+ def reset!
25
+ @link_set = Autocomplete::LinkSet.new
26
+ end
27
+ end).new
9
28
  end
10
29
  end
11
30
 
31
+
12
32
  require 'lolita-menu/module'
13
- require 'lolita-menu/rails'
33
+ require 'lolita-menu/engine'
@@ -0,0 +1,10 @@
1
+ namespace :lolita_menu do
2
+ desc 'Generate autocomplete file'
3
+ task :generate do
4
+ if File.exist?(Lolita::Menu::Autocomplete::FileBuilder.input_file)
5
+ Lolita::Menu::Autocomplete.generate
6
+ else
7
+ raise "Lolita menu URL's generator file not found. Call `rails g lolita_menu:install`"
8
+ end
9
+ end
10
+ end
data/lolita-menu.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lolita-menu}
8
- s.version = "0.0.11"
8
+ s.version = "0.0.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["ITHouse", "Arturs Meisters"]
12
- s.date = %q{2011-08-11}
11
+ s.authors = [%q{ITHouse}, %q{Arturs Meisters}]
12
+ s.date = %q{2011-08-23}
13
13
  s.description = %q{Manage public menus for each project inside Lolita.}
14
14
  s.email = %q{support@ithouse.lv}
15
15
  s.extra_rdoc_files = [
@@ -38,16 +38,38 @@ Gem::Specification.new do |s|
38
38
  "config/routes.rb",
39
39
  "lib/generators/lolita_menu/assets_generator.rb",
40
40
  "lib/generators/lolita_menu/install_generator.rb",
41
+ "lib/generators/lolita_menu/templates/lolita-menu-urls.rb",
41
42
  "lib/generators/lolita_menu/templates/migrations/create_menu_items.rb",
42
43
  "lib/generators/lolita_menu/templates/migrations/create_menus.rb",
43
44
  "lib/lolita-menu.rb",
45
+ "lib/lolita-menu/autocomplete/collector.rb",
46
+ "lib/lolita-menu/autocomplete/file_builder.rb",
47
+ "lib/lolita-menu/autocomplete/link_set.rb",
48
+ "lib/lolita-menu/engine.rb",
44
49
  "lib/lolita-menu/module.rb",
45
- "lib/lolita-menu/rails.rb",
50
+ "lib/lolita-menu/railtie.rb",
51
+ "lib/tasks/lolita-menu.rake",
46
52
  "lolita-menu.gemspec",
47
53
  "public/images/lolita-menu/delete.png",
48
54
  "public/images/lolita-menu/move.png",
55
+ "public/javascripts/autocomplete-rails.js",
49
56
  "public/javascripts/jquery-ui-nested-sortables.js",
50
57
  "public/javascripts/lolita/menu-items.js",
58
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_0_aaaaaa_40x100.png",
59
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_0_eeeeee_40x100.png",
60
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_55_c0402a_40x100.png",
61
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_flat_55_eeeeee_40x100.png",
62
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_100_f8f8f8_1x400.png",
63
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_35_dddddd_1x400.png",
64
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_glass_60_eeeeee_1x400.png",
65
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_inset-hard_75_999999_1x100.png",
66
+ "public/stylesheets/jquery-ui/overcast/images/ui-bg_inset-soft_50_c9c9c9_1x100.png",
67
+ "public/stylesheets/jquery-ui/overcast/images/ui-icons_3383bb_256x240.png",
68
+ "public/stylesheets/jquery-ui/overcast/images/ui-icons_454545_256x240.png",
69
+ "public/stylesheets/jquery-ui/overcast/images/ui-icons_70b2e1_256x240.png",
70
+ "public/stylesheets/jquery-ui/overcast/images/ui-icons_999999_256x240.png",
71
+ "public/stylesheets/jquery-ui/overcast/images/ui-icons_fbc856_256x240.png",
72
+ "public/stylesheets/jquery-ui/overcast/jquery-ui-1.8.16.custom.css",
51
73
  "public/stylesheets/lolita/menu/style.css",
52
74
  "spec/controllers/menu_items_controller_spec.rb",
53
75
  "spec/models/menu_item_spec.rb",
@@ -62,9 +84,9 @@ Gem::Specification.new do |s|
62
84
  "test_orm/rails/config/enviroment.rb"
63
85
  ]
64
86
  s.homepage = %q{http://github.com/ithouse/lolita-menu}
65
- s.licenses = ["MIT"]
66
- s.require_paths = ["lib"]
67
- s.rubygems_version = %q{1.6.2}
87
+ s.licenses = [%q{MIT}]
88
+ s.require_paths = [%q{lib}]
89
+ s.rubygems_version = %q{1.8.8}
68
90
  s.summary = %q{Menu managing plugin for Lolita.}
69
91
  s.test_files = [
70
92
  "spec/controllers/menu_items_controller_spec.rb",
@@ -0,0 +1,113 @@
1
+ /*
2
+ * Unobtrusive autocomplete
3
+ *
4
+ * To use it, you just have to include the HTML attribute autocomplete
5
+ * with the autocomplete URL as the value
6
+ *
7
+ * Example:
8
+ * <input type="text" data-autocomplete="/url/to/autocomplete">
9
+ *
10
+ * Optionally, you can use a jQuery selector to specify a field that can
11
+ * be updated with the element id whenever you find a matching value
12
+ *
13
+ * Example:
14
+ * <input type="text" data-autocomplete="/url/to/autocomplete" id_element="#id_field">
15
+ */
16
+
17
+ $(document).ready(function(){
18
+ $('input[data-autocomplete]').railsAutocomplete();
19
+ });
20
+
21
+ (function(jQuery)
22
+ {
23
+ var self = null;
24
+ jQuery.fn.railsAutocomplete = function() {
25
+ return this.live('focus',function() {
26
+ if (!this.railsAutoCompleter) {
27
+ this.railsAutoCompleter = new jQuery.railsAutocomplete(this);
28
+ }
29
+ });
30
+ };
31
+
32
+ jQuery.railsAutocomplete = function (e) {
33
+ _e = e;
34
+ this.init(_e);
35
+ };
36
+
37
+ jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
38
+ railsAutocomplete: '0.0.1'
39
+ };
40
+
41
+ jQuery.railsAutocomplete.fn.extend = jQuery.railsAutocomplete.extend = jQuery.extend;
42
+ jQuery.railsAutocomplete.fn.extend({
43
+ init: function(e) {
44
+ e.delimiter = $(e).attr('data-delimiter') || null;
45
+ function split( val ) {
46
+ return val.split( e.delimiter );
47
+ }
48
+ function extractLast( term ) {
49
+ return split( term ).pop().replace(/^\s+/,"");
50
+ }
51
+
52
+ $(e).autocomplete({
53
+ source: function( request, response ) {
54
+ $.getJSON( $(e).attr('data-autocomplete'), {
55
+ term: extractLast( request.term )
56
+ }, function() {
57
+ $(arguments[0]).each(function(i, el) {
58
+ var obj = {};
59
+ obj[el.id] = el;
60
+ $(e).data(obj);
61
+ });
62
+ response.apply(null, arguments);
63
+ });
64
+ },
65
+ search: function() {
66
+ // custom minLength
67
+ var term = extractLast( this.value );
68
+ if ( term.length < 2 ) {
69
+ return false;
70
+ }
71
+ },
72
+ focus: function() {
73
+ // prevent value inserted on focus
74
+ return false;
75
+ },
76
+ select: function( event, ui ) {
77
+ var terms = split( this.value );
78
+ // remove the current input
79
+ terms.pop();
80
+ // add the selected item
81
+ terms.push( ui.item.value );
82
+ // add placeholder to get the comma-and-space at the end
83
+ if (e.delimiter != null) {
84
+ terms.push( "" );
85
+ this.value = terms.join( e.delimiter );
86
+ } else {
87
+ this.value = terms.join("");
88
+ if ($(this).attr('id_element')) {
89
+ $($(this).attr('id_element')).val(ui.item.id);
90
+ }
91
+ if ($(this).attr('data-update-elements')) {
92
+ var data = $(this).data(ui.item.id.toString());
93
+ var update_elements = $.parseJSON($(this).attr("data-update-elements"));
94
+ for (var key in update_elements) {
95
+ $(update_elements[key]).val(data[key]);
96
+ }
97
+ }
98
+ }
99
+ var remember_string = this.value;
100
+ $(this).bind('keyup.clearId', function(){
101
+ if($(this).val().trim() != remember_string.trim()){
102
+ $($(this).attr('id_element')).val("");
103
+ $(this).unbind('keyup.clearId');
104
+ }
105
+ });
106
+ $(this).trigger('railsAutocomplete.select');
107
+
108
+ return false;
109
+ }
110
+ });
111
+ }
112
+ });
113
+ })(jQuery);