bhf 0.2.0 → 0.2.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.
@@ -1,6 +1,6 @@
1
1
  class Bhf::EntriesController < Bhf::ApplicationController
2
2
  before_filter :load_platform, :load_model, :set_page, :set_quick_edit
3
- before_filter :load_object, :except => [:create, :new]
3
+ before_filter :load_object, :except => [:create, :new, :sort]
4
4
 
5
5
  def new
6
6
  @object = @model.new
@@ -54,6 +54,17 @@ class Bhf::EntriesController < Bhf::ApplicationController
54
54
  end
55
55
  end
56
56
 
57
+ def sort
58
+ return unless @platform.sortable
59
+
60
+ sortable = @platform.sortable.to_sym
61
+
62
+ params[:order].each_with_index do |id, index|
63
+ object = @model.find(id[1].gsub("_#{@platform.name}", ''))
64
+ object.update_attribute(sortable, index)
65
+ end
66
+ end
67
+
57
68
  def destroy
58
69
  @object.destroy
59
70
  redirect_back_or_default(bhf_root_url, :notice => set_message('destory.success', @model))
@@ -15,7 +15,7 @@ class Bhf::PagesController < Bhf::ApplicationController
15
15
  @platforms = platform_options.each_with_object([]) do |opts, obj|
16
16
  platform = Bhf::Platform.new(opts, @page, current_account)
17
17
  platform.pagination = Bhf::Pagination.new(platform.entries_per_page)
18
- platform.paginated_objects = paginate_platform_objects(platform)
18
+ paginate_platform_objects(platform)
19
19
  obj << platform
20
20
  end
21
21
  end
@@ -30,7 +30,7 @@ class Bhf::PagesController < Bhf::ApplicationController
30
30
  @platform = @config.find_platform(platform_name, current_account)
31
31
 
32
32
  @platform.pagination = Bhf::Pagination.new(@platform.entries_per_page)
33
- @platform.paginated_objects = paginate_platform_objects(@platform)
33
+ paginate_platform_objects(@platform)
34
34
 
35
35
  render '_platform', :layout => false
36
36
  end
@@ -1,4 +1,4 @@
1
- - if defined?(current_account)
1
+ - if defined?(current_account) && current_account
2
2
  -# TODO: i18n and custom logout path
3
3
  %p.user
4
4
  You are logged in as
@@ -1,2 +1,2 @@
1
1
  = node f, field do
2
- = f.text_field field.name, :class => 'date'
2
+ = f.text_field field.name, :class => "datepicker #{field.type}", :value => f.object.send(field.name).to_i
File without changes
@@ -1,5 +1,5 @@
1
1
  - @platform.pagination.template = self
2
- %table{:class => (@platform.custom_columns? ? 'custom_columns' : 'default_columns')}
2
+ %table{:id => "#{@platform.name}_platform", :class => (@platform.custom_columns? ? 'custom_columns' : 'default_columns')}
3
3
  %caption
4
4
  %h4= @platform.title
5
5
  %p.info= @platform.pagination.info @platform, :entry_name => 'entry'
@@ -15,6 +15,8 @@
15
15
 
16
16
  %thead
17
17
  %tr
18
+ - if @platform.sortable
19
+ %th
18
20
  - @platform.columns.each do |column|
19
21
  %th{:class => "#{column.field.macro} #{column.field.display_type} #{order_class(column.name, @platform.name)}"}
20
22
  - column_name = @platform.model.human_attribute_name(column.name)
@@ -24,23 +26,25 @@
24
26
  %th.action
25
27
  %tfoot
26
28
  %tr
27
- %td{:colspan => @platform.columns.count+1}
28
- = @platform.pagination.paginate @platform
29
- %tbody
30
- - if @platform.paginated_objects.any?
31
- - @platform.paginated_objects.each do |object|
29
+ %td{:colspan => @platform.columns_count}
30
+ = @platform.pagination.create(@platform) unless @platform.sortable
31
+ %tbody{:class => ('sortable' if @platform.sortable), :'data-sort-url' => sort_bhf_entries_path(@platform.name)}
32
+ - if @platform.objects.any?
33
+ - @platform.objects.each do |object|
32
34
  - edit_link = edit_bhf_entry_path(@platform.name, object)
33
- %tr
35
+ %tr{:id => "#{object.id}_#{@platform.name}"}
36
+ - if @platform.sortable
37
+ %td
38
+ %span.handle &nbsp;
34
39
  - @platform.columns.each do |column|
35
40
  %td{:class => "#{column.field.macro} #{column.field.display_type}", :'data-column-name' => column.name}
36
- - unless column.field.macro == :column && object.send(column.name).blank?
37
- - p = "bhf/pages/macro/#{column.field.macro}/#{column.field.display_type}"
38
- - a = render :partial => p, :locals => {:column => column, :object => object}
39
- = column.field.overwrite_display_type ? a : link_to(a.html_safe, edit_link, :class => 'quick_edit')
41
+ - p = "bhf/pages/macro/#{column.field.macro}/#{column.field.display_type}"
42
+ - a = object.send(column.name).blank? ? '&nbsp;' : render(:partial => p, :locals => {:column => column, :object => object})
43
+ = column.field.overwrite_display_type ? a.html_safe : link_to(a.html_safe, edit_link, :class => 'quick_edit')
40
44
  %td.action
41
45
  = link_to edit_t(@platform), edit_link, :class => 'edit'
42
46
  = link_to delete_t(@platform), bhf_entry_path(@platform.name, object), :method => :delete, :confirm => t('bhf.helpers.promts.confirm'), :class => 'delete'
43
47
  - else
44
- %td.no_entries{:colspan => @platform.columns.count+1}
48
+ %td.no_entries{:colspan => @platform.columns_count}
45
49
  = t 'bhf.pagination.info.nothing_found', :name => t('bhf.pagination.entry').pluralize
46
50
  = link_to new_t(@platform), new_bhf_entry_path(@platform.name)
@@ -7,7 +7,7 @@
7
7
  %link{:href => '/favicon.ico', :rel => 'icon', :type => 'image/x-icon'}
8
8
  = stylesheet_link_tag(['bhf'] + Bhf::Engine.config.css.to_a)
9
9
  = javascript_include_tag 'bhf'
10
- =# javascript_include_tag 'mootools-core-1.3-full-nocompat-yc.js', 'mootools-more', 'mootools_rails_driver-0.4.1.js', 'class/BrowserUpdate', 'class/Ajaxify', 'class/AjaxEdit', 'class/MooEditable', 'bhf_application'
10
+ =# javascript_include_tag 'mootools-core-1.3.1-full-compat-yc.js', 'mootools-more-1.3.1.1.js', 'mootools_rails_driver-0.4.1.js', 'class/BrowserUpdate', 'class/Ajaxify', 'class/AjaxEdit', 'class/MooEditable', 'class/Datepicker', 'bhf_application'
11
11
  %body
12
12
  %header
13
13
  -# TODO: logo as css bg for better cache and no jumping?
data/config/routes.rb CHANGED
@@ -6,7 +6,11 @@ Rails.application.routes.draw do
6
6
  get 'page/:page', :to => 'pages#show', :as => :page
7
7
 
8
8
  scope ':platform' do
9
- resources :entries, :except => [:index, :show]
9
+ resources :entries, :except => [:index, :show] do
10
+ collection do
11
+ get :sort
12
+ end
13
+ end
10
14
  end
11
15
 
12
16
  end
@@ -10,17 +10,17 @@ module Bhf
10
10
  @offset_to_add = offset_to_add
11
11
  end
12
12
 
13
- def paginate(platform)
13
+ def create(platform)
14
14
  platform_params = template.params[platform.name] || {}
15
15
 
16
- if page_links = template.will_paginate(platform.paginated_objects, {
16
+ if page_links = template.will_paginate(platform.objects, {
17
17
  :previous_label => I18n.t('bhf.pagination.previous_label'),
18
18
  :next_label => I18n.t('bhf.pagination.next_label'),
19
19
  :renderer => LinkRenderer.new(self, platform),
20
20
  :container => false
21
21
  })
22
22
  links = "#{load_more(platform)} #{page_links}"
23
- elsif platform.paginated_objects.total_pages == 1 && platform.paginated_objects.size > @offset_to_add
23
+ elsif platform.objects.total_pages == 1 && platform.objects.size > @offset_to_add
24
24
  links = load_less(platform)
25
25
  end
26
26
 
@@ -30,7 +30,11 @@ module Bhf
30
30
  end
31
31
 
32
32
  def info(platform, options = {})
33
- collection = platform.paginated_objects
33
+ collection = platform.objects
34
+
35
+ unless collection.respond_to?(:total_pages)
36
+ collection = collection.paginate({:page => 1, :per_page => collection.count+1})
37
+ end
34
38
 
35
39
  entry_name = options[:entry_name] ||
36
40
  (collection.empty?? I18n.t('bhf.pagination.entry') : collection.first.class.model_name.human)
data/lib/bhf/platform.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  module Bhf
2
2
  class Platform
3
3
 
4
- attr_accessor :paginated_objects, :pagination
5
- attr_reader :name, :title, :page_name
4
+ attr_accessor :pagination
5
+ attr_reader :name, :objects, :title, :page_name
6
6
 
7
7
  def initialize(options, page_name, user = nil)
8
- @paginated_objects = []
8
+ @objects = []
9
9
 
10
10
  if options.is_a?(String)
11
11
  options = {options => nil}
@@ -46,22 +46,25 @@ module Bhf
46
46
  chain = @user.send(table_options(:user_scope).to_sym)
47
47
  else
48
48
  chain = model
49
- chain = chain.unscoped if options[:order]
50
- chain = chain.send(data_source) if data_source
49
+ chain = chain.send data_source if data_source
51
50
  end
52
51
 
53
52
  if options[:order]
54
- chain = chain.order("#{options[:order]} #{options[:direction]}")
53
+ chain = chain.except(:order).order("#{options[:order]} #{options[:direction]}")
55
54
  end
56
55
 
57
56
  if search? && options[:search].present?
58
57
  chain = do_search(chain, options[:search])
59
58
  end
60
59
 
61
- if paginate_options
60
+ if paginate_options && !sortable
62
61
  chain = chain.paginate(paginate_options)
63
62
  end
64
-
63
+
64
+ if chain == model
65
+ chain = chain.all
66
+ end
67
+
65
68
  @objects = chain
66
69
  end
67
70
 
@@ -110,6 +113,14 @@ module Bhf
110
113
  end
111
114
  end
112
115
 
116
+ def columns_count
117
+ columns.count + (sortable ? 2 : 1)
118
+ end
119
+
120
+ def sortable
121
+ table_options 'sortable'
122
+ end
123
+
113
124
  private
114
125
 
115
126
  def do_search(chain, search_term)
@@ -135,7 +146,7 @@ module Bhf
135
146
  @collection.select{ |field| attr_name == field.name }[0] ||
136
147
  Bhf::Data::AbstractField.new({
137
148
  :name => attr_name,
138
- :type => form_options(:types, attr_name) || attr_name,
149
+ :type => table_options(:types, attr_name) || attr_name,
139
150
  :info => I18n.t("bhf.platforms.#{@name}.infos.#{attr_name}", :default => '')
140
151
  })
141
152
  )
@@ -146,6 +157,7 @@ module Bhf
146
157
  all = {}
147
158
 
148
159
  model.columns_hash.each_pair do |name, props|
160
+ next if name == sortable
149
161
  all[name] = Bhf::Data::Field.new(props, {
150
162
  :overwrite_type => form_options(:types, name),
151
163
  :overwrite_display_type => table_options(:types, name),
@@ -174,7 +186,7 @@ module Bhf
174
186
  id = []
175
187
  static_dates = []
176
188
  output = []
177
-
189
+
178
190
  attrs.each_pair do |key, value|
179
191
  if key == model.primary_key
180
192
  id << value
@@ -196,25 +208,23 @@ module Bhf
196
208
  return false
197
209
  end
198
210
  end
199
- true
211
+ true
200
212
  end
201
213
 
202
214
  def form_options(key, attribute = nil)
203
- if form
204
- if attribute == nil
205
- form[key.to_s]
206
- elsif form[key.to_s]
207
- form[key.to_s][attribute.to_s]
208
- end
209
- end
215
+ lookup_options form, key, attribute
210
216
  end
211
217
 
212
218
  def table_options(key, attribute = nil)
213
- if table
219
+ lookup_options table, key, attribute
220
+ end
221
+
222
+ def lookup_options(main_key, key, attribute = nil)
223
+ if main_key
214
224
  if attribute == nil
215
- table[key.to_s]
216
- elsif table[key.to_s]
217
- table[key.to_s][attribute.to_s]
225
+ main_key[key.to_s]
226
+ elsif main_key[key.to_s]
227
+ main_key[key.to_s][attribute.to_s]
218
228
  end
219
229
  end
220
230
  end
Binary file
Binary file