livelist-rails 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,7 +75,7 @@ class window.List extends Utilities
75
75
  $(@renderTo).html( listHTML )
76
76
  @removeFetchingIndication()
77
77
 
78
- window.LiveList.version = '0.0.4'
78
+ window.LiveList.version = '0.0.5'
79
79
 
80
80
  class window.Filters extends Utilities
81
81
  constructor: (globalOptions, options = {}) ->
@@ -172,33 +172,31 @@ class window.Pagination extends Utilities
172
172
  @emptyListMessage = "<p>No #{@resourceName} matched your filter criteria</p>"
173
173
  @setOptions(options)
174
174
 
175
- $("#{@renderTo} a").live('click', @handlePaginationLinkClick)
175
+ $("#{@renderTo} a").live( 'click', (event) -> event.preventDefault() )
176
+ $("#{@renderTo} li:not(.disabled) a").live('click', @handlePaginationLinkClick)
176
177
 
177
- paginationTemplate: '''
178
+ template: '''
178
179
  {{#isEmpty}}
179
180
  {{{emptyListMessage}}}
180
181
  {{/isEmpty}}
181
182
  {{^isEmpty}}
182
- {{#previousPage}}
183
- <a href='{{urlPrefix}}?page={{previousPage}}' data-page='{{previousPage}}'>← Previous</a>
184
- {{/previousPage}}
185
- {{^previousPage}}
186
- <span>← Previous</span>
187
- {{/previousPage}}
188
- {{#pages}}
189
- {{#currentPage}}
190
- <span>{{page}}</span>
191
- {{/currentPage}}
192
- {{^currentPage}}
193
- <a href='{{urlPrefix}}?page={{page}}' data-page='{{page}}'>{{page}}</a>
194
- {{/currentPage}}
195
- {{/pages}}
196
- {{#nextPage}}
197
- <a href='{{urlPrefix}}?page={{nextPage}}' data-page='{{nextPage}}'>Next →</a>
198
- {{/nextPage}}
199
- {{^nextPage}}
200
- <span>Next →</span>
201
- {{/nextPage}}
183
+ <div class="pagination">
184
+ <ul>
185
+ <li class="{{^previousPage}}disabled{{/previousPage}}">
186
+ <a href='{{urlPrefix}}?page={{previousPage}}' data-page='{{previousPage}}'>← Previous</a>
187
+ </li>
188
+
189
+ {{#pages}}
190
+ <li class="{{#currentPage}}active disabled{{/currentPage}}">
191
+ <a href='{{urlPrefix}}?page={{page}}' data-page='{{page}}'>{{page}}</a>
192
+ </li>
193
+ {{/pages}}
194
+
195
+ <li class="{{^nextPage}}disabled{{/nextPage}}">
196
+ <a href='{{urlPrefix}}?page={{nextPage}}' data-page='{{nextPage}}'>Next →</a>
197
+ </li>
198
+ </ul>
199
+ </div>
202
200
  {{/isEmpty}}
203
201
  '''
204
202
 
@@ -225,7 +223,7 @@ class window.Pagination extends Utilities
225
223
 
226
224
  render: (data) ->
227
225
  @pagination = @paginationJSON(data.pagination)
228
- paginationHTML = Mustache.to_html(@paginationTemplate, @pagination)
226
+ paginationHTML = Mustache.to_html(@template, @pagination)
229
227
  $(@renderTo).html( paginationHTML )
230
228
 
231
229
  handlePaginationLinkClick: (event) =>
@@ -125,7 +125,7 @@
125
125
 
126
126
  })(Utilities);
127
127
 
128
- window.LiveList.version = '0.0.4';
128
+ window.LiveList.version = '0.0.5';
129
129
 
130
130
  window.Filters = (function(_super) {
131
131
 
@@ -232,10 +232,13 @@
232
232
  this.setOptions(globalOptions);
233
233
  this.emptyListMessage = "<p>No " + this.resourceName + " matched your filter criteria</p>";
234
234
  this.setOptions(options);
235
- $("" + this.renderTo + " a").live('click', this.handlePaginationLinkClick);
235
+ $("" + this.renderTo + " a").live('click', function(event) {
236
+ return event.preventDefault();
237
+ });
238
+ $("" + this.renderTo + " li:not(.disabled) a").live('click', this.handlePaginationLinkClick);
236
239
  }
237
240
 
238
- Pagination.prototype.paginationTemplate = '{{#isEmpty}}\n {{{emptyListMessage}}}\n{{/isEmpty}}\n{{^isEmpty}}\n{{#previousPage}}\n <a href=\'{{urlPrefix}}?page={{previousPage}}\' data-page=\'{{previousPage}}\'>← Previous</a>\n{{/previousPage}}\n{{^previousPage}}\n <span>← Previous</span>\n{{/previousPage}}\n{{#pages}}\n {{#currentPage}}\n <span>{{page}}</span>\n {{/currentPage}}\n {{^currentPage}}\n <a href=\'{{urlPrefix}}?page={{page}}\' data-page=\'{{page}}\'>{{page}}</a>\n {{/currentPage}}\n{{/pages}}\n{{#nextPage}}\n <a href=\'{{urlPrefix}}?page={{nextPage}}\' data-page=\'{{nextPage}}\'>Next →</a>\n{{/nextPage}}\n{{^nextPage}}\n <span>Next →</span>\n{{/nextPage}}\n{{/isEmpty}}';
241
+ Pagination.prototype.template = '{{#isEmpty}}\n {{{emptyListMessage}}}\n{{/isEmpty}}\n{{^isEmpty}}\n<div class="pagination">\n <ul>\n <li class="{{^previousPage}}disabled{{/previousPage}}">\n <a href=\'{{urlPrefix}}?page={{previousPage}}\' data-page=\'{{previousPage}}\'>← Previous</a>\n </li>\n\n {{#pages}}\n <li class="{{#currentPage}}active disabled{{/currentPage}}">\n <a href=\'{{urlPrefix}}?page={{page}}\' data-page=\'{{page}}\'>{{page}}</a>\n </li>\n {{/pages}}\n\n <li class="{{^nextPage}}disabled{{/nextPage}}">\n <a href=\'{{urlPrefix}}?page={{nextPage}}\' data-page=\'{{nextPage}}\'>Next →</a>\n </li>\n </ul>\n</div>\n{{/isEmpty}}';
239
242
 
240
243
  Pagination.prototype.pagesJSON = function(currentPage, totalPages) {
241
244
  var firstPage, groupSize, lastPage, previousPage, _i, _results;
@@ -272,7 +275,7 @@
272
275
  Pagination.prototype.render = function(data) {
273
276
  var paginationHTML;
274
277
  this.pagination = this.paginationJSON(data.pagination);
275
- paginationHTML = Mustache.to_html(this.paginationTemplate, this.pagination);
278
+ paginationHTML = Mustache.to_html(this.template, this.pagination);
276
279
  return $(this.renderTo).html(paginationHTML);
277
280
  };
278
281
 
@@ -1 +1 @@
1
- ((function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b=Object.prototype.hasOwnProperty,c=function(a,c){function e(){this.constructor=a}for(var d in c)b.call(c,d)&&(a[d]=c[d]);return e.prototype=c.prototype,a.prototype=new e,a.__super__=c.prototype,a};window.Utilities=function(){function b(){this.setOptions=a(this.setOptions,this)}return b.prototype.setOptions=function(a,b){var c=this;return b==null&&(b=this),_.each(a,function(a,c){return b[c]=a})},b}(),window.LiveList=function(a){function b(a){this.globalOptions.listSelector=a.list.renderTo,this.globalOptions.eventName="livelist:"+a.global.resourceName,this.globalOptions.urlPrefix="/"+a.global.resourceName,this.setOptions(a.global,this.globalOptions),this.search=new Search(this.globalOptions,a.search),this.filters=new Filters(this.globalOptions,a.filters),this.pagination=new Pagination(this.globalOptions,a.pagination),this.list=new List(this.search,this.filters,this.pagination,this.globalOptions,a.list)}return c(b,a),b.prototype.globalOptions={data:null,resourceName:"items",resourceNameSingular:"item"},b}(Utilities),window.List=function(b){function d(b,c,d,e,f){var g,h=this;f==null&&(f={}),this.renderIndex=a(this.renderIndex,this),this.removeFetchingIndication=a(this.removeFetchingIndication,this),this.displayFetchingIndication=a(this.displayFetchingIndication,this),this.data=e.data,this.fetchRequest=null,this.search=b,this.filters=c,this.pagination=d,this.setOptions(e),this.listTemplate="{{#"+this.resourceName+"}}{{>"+this.resourceNameSingular+"}}{{/"+this.resourceName+"}}",this.listItemTemplate="<li>{{id}}</li>",this.fetchingIndicationClass="updating",this.setOptions(f),$(this.renderTo).bind(this.eventName,function(a,b){return h.fetch({presets:null,page:b!=null?b.page:void 0})}),g=this.filters.presets(),this.fetch({presets:g})}return c(d,b),d.prototype.displayFetchingIndication=function(){return $(this.renderTo).addClass(this.fetchingIndicationClass)},d.prototype.removeFetchingIndication=function(){return $(this.renderTo).removeClass(this.fetchingIndicationClass)},d.prototype.renderIndex=function(a,b,c){return this.data=a,this.render(),this.pagination.render(this.data),this.filters.render(this.data)},d.prototype.fetch=function(a){var b,c;return this.fetchRequest&&this.fetchRequest.abort(),c=this.search.searchTerm(),b={},b.filters=this.filters.setPresets(a.presets),c&&(b.q=c),a.page&&(b.page=a.page),this.fetchRequest=$.ajax({url:this.urlPrefix,dataType:"json",data:b,type:this.httpMethod,beforeSend:this.displayFetchingIndication,success:this.renderIndex})},d.prototype.render=function(){var a,b;return b={},b[this.resourceNameSingular]=this.listItemTemplate,a=Mustache.to_html(this.listTemplate,this.data,b),$(this.renderTo).html(a),this.removeFetchingIndication()},d}(Utilities),window.LiveList.version="0.0.4",window.Filters=function(b){function d(b,c){var d=this;c==null&&(c={}),this.handleAdvancedOptionsClick=a(this.handleAdvancedOptionsClick,this),this.setOptions(b),this.filters=c.presets?_.keys(c.presets):[],this.initializeCookies(),this.setOptions(c),$("input.filter_option",this.renderTo).live("change",function(){return $(d.listSelector).trigger(d.eventName)}),$(this.advancedOptionsToggleSelector).click(this.handleAdvancedOptionsClick)}return c(d,b),d.prototype.initializeCookies=function(){if(jQuery.cookie&&this.useCookies&&this.cookieName)return this.cookieName="livelist_filter_presets"},d.prototype.presets=function(){var a;return jQuery.cookie&&this.useCookies&&(a=jQuery.cookie(this.cookieName)),this.useCookies&&a?JSON.parse(a):this.presets},d.prototype.setPresets=function(a){var b;return b={},jQuery.isEmptyObject(a)?(b=this.selections(),jQuery.cookie&&this.setCookie()):b=a,b},d.prototype.setCookie=function(a){if(!jQuery.isEmptyObject(a))return jQuery.cookie(this.cookieName,JSON.stringify(a))},d.prototype.template="{{#filters}}\n<div class='filter'>\n <h3>\n {{name}}\n </h3>\n <ul id='{{filter_slug}}_filter_options'>\n {{#options}}\n <label>\n <li>\n <input {{#selected}}checked='checked'{{/selected}}\n class='left filter_option'\n id='filter_{{slug}}'\n name='filters[]'\n type='checkbox'\n value='{{value}}' />\n <div class='left filter_name'>{{name}}</div>\n <div class='right filter_count'>{{count}}</div>\n <div class='clear'></div>\n </li>\n </label>\n {{/options}}\n </ul>\n</div>\n{{/filters}}",d.prototype.selections=function(){var a,b=this;return a={},_.each(this.filters,function(b){return a[b]=_.pluck($("#"+b+"_filter_options input.filter_option:checked"),"value")}),a},d.prototype.noFiltersSelected=function(a){return _.all(a.filters,function(a){return _.all(a.options,function(a){return!a.selected})})},d.prototype.render=function(a){var b;this.filters=_.pluck(a.filters,"filter_slug"),b=Mustache.to_html(this.template,a),$(this.renderTo).html(b);if(this.noFiltersSelected(a)&&a[this.resourceName].length>0)return $('input[type="checkbox"]',this.renderTo).attr("checked","checked")},d.prototype.handleAdvancedOptionsClick=function(a){return a.preventDefault(),$(this.renderTo).slideToggle()},d}(Utilities),window.Pagination=function(b){function d(b,c){c==null&&(c={}),this.handlePaginationLinkClick=a(this.handlePaginationLinkClick,this),this.pagination=null,this.maxPages=30,this.setOptions(b),this.emptyListMessage="<p>No "+this.resourceName+" matched your filter criteria</p>",this.setOptions(c),$(""+this.renderTo+" a").live("click",this.handlePaginationLinkClick)}return c(d,b),d.prototype.paginationTemplate="{{#isEmpty}}\n {{{emptyListMessage}}}\n{{/isEmpty}}\n{{^isEmpty}}\n{{#previousPage}}\n <a href='{{urlPrefix}}?page={{previousPage}}' data-page='{{previousPage}}'>← Previous</a>\n{{/previousPage}}\n{{^previousPage}}\n <span>← Previous</span>\n{{/previousPage}}\n{{#pages}}\n {{#currentPage}}\n <span>{{page}}</span>\n {{/currentPage}}\n {{^currentPage}}\n <a href='{{urlPrefix}}?page={{page}}' data-page='{{page}}'>{{page}}</a>\n {{/currentPage}}\n{{/pages}}\n{{#nextPage}}\n <a href='{{urlPrefix}}?page={{nextPage}}' data-page='{{nextPage}}'>Next →</a>\n{{/nextPage}}\n{{^nextPage}}\n <span>Next →</span>\n{{/nextPage}}\n{{/isEmpty}}",d.prototype.pagesJSON=function(a,b){var c,d,e,f,g,h;return d=this.maxPages/2,c=a<d?1:a-d,f=c+d*2-1,e=f>=b?b:f,_.map(function(){h=[];for(var a=c;c<=e?a<=e:a>=e;c<=e?a++:a--)h.push(a);return h}.apply(this),function(b){return{page:b,currentPage:function(){return a===b}}})},d.prototype.paginationJSON=function(a){return{isEmpty:a.total_pages===0,emptyListMessage:this.emptyListMessage,currentPage:a.current_page,nextPage:a.next_page,previousPage:a.previous_page,urlPrefix:this.urlPrefix,pages:this.pagesJSON(a.current_page,a.total_pages)}},d.prototype.render=function(a){var b;return this.pagination=this.paginationJSON(a.pagination),b=Mustache.to_html(this.paginationTemplate,this.pagination),$(this.renderTo).html(b)},d.prototype.handlePaginationLinkClick=function(a){return a.preventDefault(),$(this.listSelector).trigger(this.eventName,{page:$(a.target).data("page")})},d}(Utilities),window.Search=function(b){function d(b,c){var d=this;c==null&&(c={}),this.handleSearchFormSubmit=a(this.handleSearchFormSubmit,this),this.setOptions(b),this.setOptions(c),$(this.formSelector).submit(function(a){return d.handleSearchFormSubmit(a)})}return c(d,b),d.prototype.searchTerm=function(){var a;return a=$(this.searchTextInputSelector).val(),!a||a===""?null:a},d.prototype.handleSearchFormSubmit=function(a){return a.preventDefault(),$(this.listSelector).trigger(this.eventName)},d}(Utilities)})).call(this);
1
+ ((function(){var a=function(a,b){return function(){return a.apply(b,arguments)}},b=Object.prototype.hasOwnProperty,c=function(a,c){function e(){this.constructor=a}for(var d in c)b.call(c,d)&&(a[d]=c[d]);return e.prototype=c.prototype,a.prototype=new e,a.__super__=c.prototype,a};window.Utilities=function(){function b(){this.setOptions=a(this.setOptions,this)}return b.prototype.setOptions=function(a,b){var c=this;return b==null&&(b=this),_.each(a,function(a,c){return b[c]=a})},b}(),window.LiveList=function(a){function b(a){this.globalOptions.listSelector=a.list.renderTo,this.globalOptions.eventName="livelist:"+a.global.resourceName,this.globalOptions.urlPrefix="/"+a.global.resourceName,this.setOptions(a.global,this.globalOptions),this.search=new Search(this.globalOptions,a.search),this.filters=new Filters(this.globalOptions,a.filters),this.pagination=new Pagination(this.globalOptions,a.pagination),this.list=new List(this.search,this.filters,this.pagination,this.globalOptions,a.list)}return c(b,a),b.prototype.globalOptions={data:null,resourceName:"items",resourceNameSingular:"item"},b}(Utilities),window.List=function(b){function d(b,c,d,e,f){var g,h=this;f==null&&(f={}),this.renderIndex=a(this.renderIndex,this),this.removeFetchingIndication=a(this.removeFetchingIndication,this),this.displayFetchingIndication=a(this.displayFetchingIndication,this),this.data=e.data,this.fetchRequest=null,this.search=b,this.filters=c,this.pagination=d,this.setOptions(e),this.listTemplate="{{#"+this.resourceName+"}}{{>"+this.resourceNameSingular+"}}{{/"+this.resourceName+"}}",this.listItemTemplate="<li>{{id}}</li>",this.fetchingIndicationClass="updating",this.setOptions(f),$(this.renderTo).bind(this.eventName,function(a,b){return h.fetch({presets:null,page:b!=null?b.page:void 0})}),g=this.filters.presets(),this.fetch({presets:g})}return c(d,b),d.prototype.displayFetchingIndication=function(){return $(this.renderTo).addClass(this.fetchingIndicationClass)},d.prototype.removeFetchingIndication=function(){return $(this.renderTo).removeClass(this.fetchingIndicationClass)},d.prototype.renderIndex=function(a,b,c){return this.data=a,this.render(),this.pagination.render(this.data),this.filters.render(this.data)},d.prototype.fetch=function(a){var b,c;return this.fetchRequest&&this.fetchRequest.abort(),c=this.search.searchTerm(),b={},b.filters=this.filters.setPresets(a.presets),c&&(b.q=c),a.page&&(b.page=a.page),this.fetchRequest=$.ajax({url:this.urlPrefix,dataType:"json",data:b,type:this.httpMethod,beforeSend:this.displayFetchingIndication,success:this.renderIndex})},d.prototype.render=function(){var a,b;return b={},b[this.resourceNameSingular]=this.listItemTemplate,a=Mustache.to_html(this.listTemplate,this.data,b),$(this.renderTo).html(a),this.removeFetchingIndication()},d}(Utilities),window.LiveList.version="0.0.5",window.Filters=function(b){function d(b,c){var d=this;c==null&&(c={}),this.handleAdvancedOptionsClick=a(this.handleAdvancedOptionsClick,this),this.setOptions(b),this.filters=c.presets?_.keys(c.presets):[],this.initializeCookies(),this.setOptions(c),$("input.filter_option",this.renderTo).live("change",function(){return $(d.listSelector).trigger(d.eventName)}),$(this.advancedOptionsToggleSelector).click(this.handleAdvancedOptionsClick)}return c(d,b),d.prototype.initializeCookies=function(){if(jQuery.cookie&&this.useCookies&&this.cookieName)return this.cookieName="livelist_filter_presets"},d.prototype.presets=function(){var a;return jQuery.cookie&&this.useCookies&&(a=jQuery.cookie(this.cookieName)),this.useCookies&&a?JSON.parse(a):this.presets},d.prototype.setPresets=function(a){var b;return b={},jQuery.isEmptyObject(a)?(b=this.selections(),jQuery.cookie&&this.setCookie()):b=a,b},d.prototype.setCookie=function(a){if(!jQuery.isEmptyObject(a))return jQuery.cookie(this.cookieName,JSON.stringify(a))},d.prototype.template="{{#filters}}\n<div class='filter'>\n <h3>\n {{name}}\n </h3>\n <ul id='{{filter_slug}}_filter_options'>\n {{#options}}\n <label>\n <li>\n <input {{#selected}}checked='checked'{{/selected}}\n class='left filter_option'\n id='filter_{{slug}}'\n name='filters[]'\n type='checkbox'\n value='{{value}}' />\n <div class='left filter_name'>{{name}}</div>\n <div class='right filter_count'>{{count}}</div>\n <div class='clear'></div>\n </li>\n </label>\n {{/options}}\n </ul>\n</div>\n{{/filters}}",d.prototype.selections=function(){var a,b=this;return a={},_.each(this.filters,function(b){return a[b]=_.pluck($("#"+b+"_filter_options input.filter_option:checked"),"value")}),a},d.prototype.noFiltersSelected=function(a){return _.all(a.filters,function(a){return _.all(a.options,function(a){return!a.selected})})},d.prototype.render=function(a){var b;this.filters=_.pluck(a.filters,"filter_slug"),b=Mustache.to_html(this.template,a),$(this.renderTo).html(b);if(this.noFiltersSelected(a)&&a[this.resourceName].length>0)return $('input[type="checkbox"]',this.renderTo).attr("checked","checked")},d.prototype.handleAdvancedOptionsClick=function(a){return a.preventDefault(),$(this.renderTo).slideToggle()},d}(Utilities),window.Pagination=function(b){function d(b,c){c==null&&(c={}),this.handlePaginationLinkClick=a(this.handlePaginationLinkClick,this),this.pagination=null,this.maxPages=30,this.setOptions(b),this.emptyListMessage="<p>No "+this.resourceName+" matched your filter criteria</p>",this.setOptions(c),$(""+this.renderTo+" a").live("click",function(a){return a.preventDefault()}),$(""+this.renderTo+" li:not(.disabled) a").live("click",this.handlePaginationLinkClick)}return c(d,b),d.prototype.template="{{#isEmpty}}\n {{{emptyListMessage}}}\n{{/isEmpty}}\n{{^isEmpty}}\n<div class=\"pagination\">\n <ul>\n <li class=\"{{^previousPage}}disabled{{/previousPage}}\">\n <a href='{{urlPrefix}}?page={{previousPage}}' data-page='{{previousPage}}'>← Previous</a>\n </li>\n\n {{#pages}}\n <li class=\"{{#currentPage}}active disabled{{/currentPage}}\">\n <a href='{{urlPrefix}}?page={{page}}' data-page='{{page}}'>{{page}}</a>\n </li>\n {{/pages}}\n\n <li class=\"{{^nextPage}}disabled{{/nextPage}}\">\n <a href='{{urlPrefix}}?page={{nextPage}}' data-page='{{nextPage}}'>Next →</a>\n </li>\n </ul>\n</div>\n{{/isEmpty}}",d.prototype.pagesJSON=function(a,b){var c,d,e,f,g,h;return d=this.maxPages/2,c=a<d?1:a-d,f=c+d*2-1,e=f>=b?b:f,_.map(function(){h=[];for(var a=c;c<=e?a<=e:a>=e;c<=e?a++:a--)h.push(a);return h}.apply(this),function(b){return{page:b,currentPage:function(){return a===b}}})},d.prototype.paginationJSON=function(a){return{isEmpty:a.total_pages===0,emptyListMessage:this.emptyListMessage,currentPage:a.current_page,nextPage:a.next_page,previousPage:a.previous_page,urlPrefix:this.urlPrefix,pages:this.pagesJSON(a.current_page,a.total_pages)}},d.prototype.render=function(a){var b;return this.pagination=this.paginationJSON(a.pagination),b=Mustache.to_html(this.template,this.pagination),$(this.renderTo).html(b)},d.prototype.handlePaginationLinkClick=function(a){return a.preventDefault(),$(this.listSelector).trigger(this.eventName,{page:$(a.target).data("page")})},d}(Utilities),window.Search=function(b){function d(b,c){var d=this;c==null&&(c={}),this.handleSearchFormSubmit=a(this.handleSearchFormSubmit,this),this.setOptions(b),this.setOptions(c),$(this.formSelector).submit(function(a){return d.handleSearchFormSubmit(a)})}return c(d,b),d.prototype.searchTerm=function(){var a;return a=$(this.searchTextInputSelector).val(),!a||a===""?null:a},d.prototype.handleSearchFormSubmit=function(a){return a.preventDefault(),$(this.listSelector).trigger(this.eventName)},d}(Utilities)})).call(this);
@@ -7,38 +7,68 @@ module Livelist
7
7
  @@filter_collections = {}
8
8
 
9
9
  def filter_for(filter_slug, options = {})
10
- @@counts = {}
11
-
12
10
  @@filter_slugs << filter_slug unless @@filter_slugs.include?(filter_slug)
13
- @@filter_collections[filter_slug] = options[:collection] || select("distinct #{filter_slug}").all
11
+ @@filter_collections[filter_slug] = options[:collection] || lambda { select("distinct #{filter_slug}") }
14
12
 
15
13
  define_class_methods(filter_slug)
16
14
 
17
15
  def filters_as_json(filter_params)
18
- @@filter_params = filter_params || {}
19
- @@filter_slugs.map do |filter|
16
+ @counts = {}
17
+ @filter_params = filter_params || {}
18
+ filters = @@filter_slugs.map do |filter|
20
19
  filter_options = send("#{filter}_filters", filter)
21
20
  send("#{filter}_filter", filter_options)
22
21
  end
23
22
  end
24
23
 
25
24
  def filter_relation(filter_params)
26
- relation = scoped
25
+ query = scoped
27
26
  @@filter_slugs.each do |filter_slug|
28
27
  values = filter_params[filter_slug.to_s]
29
- relation = relation.send("#{filter_slug}_relation", values) unless filter_params.empty?
28
+ query = query.send("#{filter_slug}_where", filter_collection(filter_slug))
29
+ query = query.send("#{filter_slug}_relation", values) unless filter_params.empty?
30
30
  end
31
- relation
31
+ query
32
32
  end
33
33
 
34
34
  def filter(filter_params)
35
35
  filter_params ||= {}
36
- @@filter_relation = filter_relation(filter_params)
36
+ @filter_relation = filter_relation(filter_params)
37
37
  end
38
38
 
39
39
  def filter_option_count(filter_slug, option)
40
- @@counts[filter_slug] = send("#{filter_slug}_filter_counts") unless @@counts.has_key?(filter_slug)
41
- @@counts[filter_slug][option.to_s] || 0
40
+ @counts[filter_slug] ||= send("#{filter_slug}_filter_counts")
41
+ @counts[filter_slug][option.to_s] || 0
42
+ end
43
+
44
+ def filter_collection(filter_slug)
45
+ collection = @@filter_collections[filter_slug.to_sym]
46
+ if collection.respond_to?(:call)
47
+ collection.call.map(&filter_slug.to_sym)
48
+ else
49
+ collection
50
+ end
51
+ end
52
+
53
+ def exclude_filter_relation?(filter_slug, slug)
54
+ @filter_params[filter_slug].nil? || (slug.to_s == filter_slug)
55
+ end
56
+
57
+ def counts_relation(filter_slug, slug)
58
+ query = scoped
59
+ query = query.send("#{slug}_join")
60
+ query = query.send("#{slug}_where", filter_collection(slug))
61
+ query = query.send("#{slug}_where", @filter_params[slug]) unless exclude_filter_relation?(filter_slug, slug)
62
+ query
63
+ end
64
+
65
+ def filter_slug_filter_counts(filter_slug)
66
+ query = scoped.except(:order)
67
+ @@filter_slugs.each do |slug|
68
+ query = query.counts_relation(filter_slug, slug)
69
+ end
70
+ group_by = send("#{filter_slug}_counts_group_by")
71
+ query.group(group_by).count
42
72
  end
43
73
  end
44
74
 
@@ -47,12 +77,22 @@ module Livelist
47
77
  metaclass = class << self; self; end
48
78
 
49
79
  metaclass.instance_eval do
50
- define_method "#{filter_slug}_filter_name" do
51
- filter_slug.capitalize
80
+ define_method("#{filter_slug}_filter_name") { filter_slug.capitalize }
81
+ define_method("#{filter_slug}_filter_slug") { filter_slug }
82
+ define_method("#{filter_slug}_counts_group_by") { filter_slug }
83
+ define_method("#{filter_slug}_join") { scoped }
84
+ define_method("#{filter_slug}_where") { |values| where(model_name.to_s.tableize => { filter_slug => values }) }
85
+ define_method("#{filter_slug}_filter_counts") { filter_slug_filter_counts(filter_slug) }
86
+ define_method("#{filter_slug}_filter_option_key_name") { new.respond_to?(filter_slug.to_sym) ? filter_slug.to_sym : :id }
87
+ define_method("#{filter_slug}_relation") { |values| send("#{filter_slug}_join").send("#{filter_slug}_where", values) }
88
+ define_method("#{filter_slug}_filter_option_count") { |option| filter_option_count(filter_slug, option) }
89
+
90
+ define_method("#{filter_slug}_filter_option_value") do |option|
91
+ [String, Integer].any?{|klass| option.kind_of?(klass)} ? option.to_s : option.send(:id).to_s
52
92
  end
53
93
 
54
- define_method "#{filter_slug}_filter_slug" do
55
- filter_slug
94
+ define_method("#{filter_slug}_filter_option_selected?") do |filter, option|
95
+ @filter_params[filter].nil? ? false : @filter_params[filter].include?(option.to_s)
56
96
  end
57
97
 
58
98
  define_method "#{filter_slug}_filter" do |options|
@@ -63,42 +103,18 @@ module Livelist
63
103
  }
64
104
  end
65
105
 
66
- define_method "#{filter_slug}_filter_option_key_name" do
67
- new.respond_to?(filter_slug.to_sym) ? filter_slug.to_sym : :id
68
- end
69
-
70
106
  define_method "#{filter_slug}_filter_values" do
71
107
  key = send("#{filter_slug}_filter_option_key_name")
72
- filter_collection = @@filter_collections[filter_slug.to_sym]
73
- if filter_collection.any?{|object| object.kind_of?(Hash) && object.has_key?(key)}
74
- filter_collection.map{|object| object[key]}
75
- elsif filter_collection.any?{|object| object.respond_to?(key)}
76
- filter_collection.map(&key)
108
+ collection = filter_collection(filter_slug)
109
+ if collection.any?{|object| object.kind_of?(Hash) && object.has_key?(key)}
110
+ collection.map{|object| object[key]}
111
+ elsif collection.any?{|object| object.respond_to?(key)}
112
+ collection.map(&key)
113
+ elsif collection.kind_of?(Array)
114
+ collection
77
115
  end
78
116
  end
79
117
 
80
- define_method "#{filter_slug}_counts_group_by" do
81
- filter_slug
82
- end
83
-
84
- define_method "#{filter_slug}_join" do
85
- scoped
86
- end
87
-
88
- define_method "#{filter_slug}_where" do |values|
89
- where(model_name.to_s.tableize => { filter_slug => values })
90
- end
91
-
92
- define_method "#{filter_slug}_filter_counts" do
93
- query = scoped.except(:order)
94
- @@filter_slugs.each do |slug|
95
- query = query.send("#{slug}_join")
96
- query = query.send("#{slug}_where", @@filter_params[slug]) unless @@filter_params[filter_slug].nil? || (slug.to_s == filter_slug)
97
- end
98
- group_by = send("#{filter_slug}_counts_group_by")
99
- query.group(group_by).count
100
- end
101
-
102
118
  define_method "#{filter_slug}_filter_option_slug" do |option|
103
119
  if [String, Integer].any?{|klass| option.kind_of?(klass)}
104
120
  option.to_s
@@ -108,30 +124,18 @@ module Livelist
108
124
  end
109
125
 
110
126
  define_method "#{filter_slug}_filter_option_name" do |option|
111
- filter_collection = @@filter_collections[filter_slug.to_sym]
112
- if filter_collection.any?{|object| object.kind_of?(Hash) && object.has_key?(:name)}
113
- option_object = filter_collection.detect{|object| object[:state] == option.to_s}
127
+ collection = filter_collection(filter_slug)
128
+ if collection.any?{|object| object.kind_of?(Hash) && object.has_key?(:name)}
129
+ option_object = collection.detect{|object| object[:state] == option.to_s}
114
130
  option_object[:name]
115
- elsif filter_collection.any?{|object| object.respond_to?(:name)}
116
- option_object = filter_collection.detect{|object| object.send(:id).to_s == option.to_s}
131
+ elsif collection.any?{|object| object.respond_to?(:name)}
132
+ option_object = collection.detect{|object| object.send(:id).to_s == option.to_s}
117
133
  option_object.send(:name)
118
134
  else
119
135
  option.to_s
120
136
  end
121
137
  end
122
138
 
123
- define_method "#{filter_slug}_filter_option_value" do |option|
124
- if [String, Integer].any?{|klass| option.kind_of?(klass)}
125
- option.to_s
126
- else
127
- option.send(:id).to_s
128
- end
129
- end
130
-
131
- define_method "#{filter_slug}_filter_option_count" do |option|
132
- filter_option_count(filter_slug, option)
133
- end
134
-
135
139
  define_method "#{filter_slug}_filter_option" do |option, selected|
136
140
  {
137
141
  :slug => send("#{filter_slug}_filter_option_slug", option),
@@ -142,20 +146,12 @@ module Livelist
142
146
  }
143
147
  end
144
148
 
145
- define_method "#{filter_slug}_filter_option_selected?" do |filter, option|
146
- @@filter_params[filter].nil? ? false : @@filter_params[filter].include?(option.to_s)
147
- end
148
-
149
149
  define_method "#{filter_slug}_filters" do |filter|
150
150
  send("#{filter_slug}_filter_values").map do |option|
151
151
  selected = send("#{filter_slug}_filter_option_selected?", filter, option)
152
152
  send("#{filter_slug}_filter_option", option, selected)
153
153
  end
154
154
  end
155
-
156
- define_method "#{filter_slug}_relation" do |values|
157
- send("#{filter_slug}_join").send("#{filter_slug}_where", values)
158
- end
159
155
  end
160
156
  end
161
157
  end
@@ -1,7 +1,7 @@
1
1
  module Livelist
2
2
  module Rails
3
- VERSION = '0.0.9'
4
- LIVELIST_VERSION = '0.0.4'
3
+ VERSION = '0.0.10'
4
+ LIVELIST_VERSION = '0.0.5'
5
5
  MUSTACHE_VERSION = 'db5f5ece0b6c87bbb2d0584010b97f8723dde69d'
6
6
  UNDERSCORE_VERSION = '1.2.3'
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livelist-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-01 00:00:00.000000000Z
12
+ date: 2012-02-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &16423600 !ruby/object:Gem::Requirement
16
+ requirement: &19316400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *16423600
24
+ version_requirements: *19316400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activerecord
27
- requirement: &16423180 !ruby/object:Gem::Requirement
27
+ requirement: &19315980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *16423180
35
+ version_requirements: *19315980
36
36
  description: ! 'livelist-rails is a Rails 3.1 Engine/Extensiont incorporating the
37
37
  following javascript libraries: Mustache.js, underscore.js, jQuery and livelist.js,
38
38
  and providing ActiveRecord filtering extenstions.'