bhf 0.3.10 → 0.3.11

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.
Files changed (61) hide show
  1. data/app/controllers/bhf/application_controller.rb +0 -2
  2. data/app/controllers/bhf/embed_entries_controller.rb +78 -0
  3. data/app/controllers/bhf/entries_controller.rb +7 -6
  4. data/app/controllers/bhf/pages_controller.rb +1 -0
  5. data/app/helpers/bhf/entries_helper.rb +8 -4
  6. data/app/views/bhf/entries/_form.haml +1 -1
  7. data/app/views/bhf/entries/edit.haml +1 -1
  8. data/app/views/bhf/entries/form/belongs_to/_select.haml +1 -1
  9. data/app/views/bhf/entries/form/belongs_to/_static.haml +2 -5
  10. data/app/views/bhf/entries/form/column/_array.haml +8 -0
  11. data/app/views/bhf/entries/form/embeds_many/_static.haml +14 -0
  12. data/app/views/bhf/entries/form/embeds_one/_static.haml +13 -0
  13. data/app/views/bhf/entries/form/has_and_belongs_to_many/_static.haml +2 -6
  14. data/app/views/bhf/entries/form/has_many/_static.haml +2 -6
  15. data/app/views/bhf/entries/form/has_one/_static.haml +2 -5
  16. data/app/views/bhf/entries/new.haml +1 -1
  17. data/app/views/bhf/helper/_reflection_node.haml +2 -2
  18. data/app/views/bhf/pages/_platform.haml +6 -8
  19. data/app/views/bhf/pages/macro/belongs_to/_default.haml +1 -1
  20. data/app/views/bhf/pages/macro/column/_array.haml +1 -0
  21. data/app/views/bhf/pages/macro/column/_boolean.haml +1 -1
  22. data/app/views/bhf/pages/macro/embeds_many/_default.haml +1 -0
  23. data/app/views/bhf/pages/macro/embeds_one/_default.haml +1 -0
  24. data/app/views/bhf/pages/macro/has_and_belongs_to_many/_default.haml +1 -2
  25. data/app/views/bhf/pages/macro/has_many/_default.haml +2 -2
  26. data/app/views/bhf/pages/macro/has_one/_default.haml +2 -2
  27. data/app/views/bhf/pages/show.haml +1 -1
  28. data/app/views/kaminari/bhf/_gap.html.haml +1 -0
  29. data/app/views/kaminari/bhf/_next_page.html.haml +2 -0
  30. data/app/views/kaminari/bhf/_page.html.haml +2 -0
  31. data/app/views/kaminari/bhf/_paginator.html.haml +10 -0
  32. data/app/views/kaminari/bhf/_prev_page.html.haml +2 -0
  33. data/app/views/layouts/bhf/application.haml +1 -1
  34. data/app/views/layouts/bhf/quick_edit.haml +4 -2
  35. data/config/locales/de.yml +3 -4
  36. data/config/locales/en.yml +9 -6
  37. data/config/routes.rb +1 -0
  38. data/lib/bhf.rb +1 -0
  39. data/lib/bhf/active_record/active_record.rb +11 -2
  40. data/lib/bhf/active_record/upload.rb +9 -9
  41. data/lib/bhf/data.rb +2 -2
  42. data/lib/bhf/mongoid/document.rb +138 -0
  43. data/lib/bhf/pagination.rb +42 -42
  44. data/lib/bhf/platform.rb +8 -9
  45. data/lib/engine.rb +1 -2
  46. data/public/images/bhf/pictos.png +0 -0
  47. data/public/javascripts/bhf.js +1 -1
  48. data/public/stylesheets/bhf.css +1 -1
  49. metadata +22 -23
  50. data/public/images/bhf/button_bg.png +0 -0
  51. data/public/images/bhf/button_bg_2.png +0 -0
  52. data/public/images/bhf/button_bg_a.png +0 -0
  53. data/public/images/bhf/icon_delete.png +0 -0
  54. data/public/images/bhf/icon_delete_a.png +0 -0
  55. data/public/images/bhf/icon_delete_h.png +0 -0
  56. data/public/images/bhf/icon_drag.png +0 -0
  57. data/public/images/bhf/icon_drag_a.png +0 -0
  58. data/public/images/bhf/icon_drag_h.png +0 -0
  59. data/public/images/bhf/icon_edit.png +0 -0
  60. data/public/images/bhf/icon_edit_a.png +0 -0
  61. data/public/images/bhf/icon_edit_h.png +0 -0
@@ -1,4 +1,4 @@
1
- en:
1
+ en:
2
2
  bhf:
3
3
  pages:
4
4
  headline: Content
@@ -17,8 +17,6 @@ en:
17
17
  delete: "Delete %{platform_name}"
18
18
  empty: Empty
19
19
  cancel: Cancel
20
- entries:
21
- empty: Empty
22
20
  login:
23
21
  success: "Login was successful"
24
22
  error: "Can't login"
@@ -37,20 +35,25 @@ en:
37
35
  cancel: Cancel
38
36
  open: Open
39
37
  save: Save
40
- save_and_next: Save and go to next
38
+ create: Create
39
+ save_and_next: Save and go to the next entry
41
40
 
42
41
  pagination:
43
42
  previous_label: « Previous
44
43
  next_label: Next »
44
+ truncate: …
45
45
  load_more: Show more entries
46
46
  load_less: Show less entries
47
- entry: entry
48
47
  entries: entries
49
48
  info:
50
49
  default: "Displaying <strong>%{offset_start} - %{offset_end}</strong> of <strong>%{count}</strong> %{name} in total"
51
- other: "Displaying <strong>all %{total_count}</strong> %{name_plural}"
50
+ other: "Displaying <strong>all %{count}</strong> %{name_plural}"
52
51
  one: "Displaying <strong>1</strong> %{name_singular}"
53
52
  zero: "No %{name_zero} found"
53
+
54
+ attributes:
55
+ _id: 'id'
56
+
54
57
  activerecord:
55
58
  notices:
56
59
  messages:
data/config/routes.rb CHANGED
@@ -7,6 +7,7 @@ Rails.application.routes.draw do
7
7
 
8
8
  scope ':platform' do
9
9
  resources :entries, :except => [:index, :show] do
10
+ resources :embed_entries, :except => [:index, :show], :as => :embed
10
11
  collection do
11
12
  # TODO: put it
12
13
  get :sort
data/lib/bhf.rb CHANGED
@@ -6,6 +6,7 @@ require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
6
6
  require 'bhf/i18n'
7
7
  require 'bhf/active_record/active_record'
8
8
  require 'bhf/active_record/upload'
9
+ require 'bhf/mongoid/document'
9
10
  require 'bhf/view_helpers'
10
11
  require 'bhf/data'
11
12
  require 'bhf/platform'
@@ -1,6 +1,7 @@
1
1
  module Bhf
2
2
  module ActiveRecord
3
3
  module Object
4
+
4
5
  extend ActiveSupport::Concern
5
6
 
6
7
  def to_bhf_s
@@ -16,7 +17,7 @@ module Bhf
16
17
  self.to_s.humanize
17
18
  end
18
19
 
19
- module ClassMethods
20
+ module ClassMethods
20
21
  def bhf_default_search(search_params)
21
22
  return if (search_term = search_params[:text]).blank?
22
23
  where_statement = []
@@ -34,8 +35,16 @@ module Bhf
34
35
 
35
36
  where_statement.join(' OR ')
36
37
  end
38
+
39
+ def bhf_primary_key
40
+ primary_key
41
+ end
42
+
43
+ def bhf_embedded?
44
+ false
45
+ end
37
46
  end
38
47
 
39
48
  end
40
49
  end
41
- end
50
+ end
@@ -8,14 +8,6 @@ module Bhf
8
8
  cattr_accessor :bhf_upload_settings
9
9
  end
10
10
 
11
- module ClassMethods
12
- def setup_upload(settings)
13
- self.bhf_upload_settings = settings.each_with_object([]) do |s, obj|
14
- obj << {:path => '', :name => :file}.merge(s)
15
- end
16
- end
17
- end
18
-
19
11
  def bhf_upload
20
12
  self.class.bhf_upload_settings.each do |settings|
21
13
  name_was = send("#{settings[:name]}_was")
@@ -28,7 +20,7 @@ module Bhf
28
20
  if file.is_a? ActionDispatch::Http::UploadedFile
29
21
  # File.delete(settings[:path] + name_was.to_s) if File.exist?(settings[:path] + name_was.to_s)
30
22
 
31
- filename = Time.now.to_i.to_s+'_'+file.original_filename.downcase.sub(/[^\w\.\-]/,'_')
23
+ filename = Time.now.to_i.to_s+'_'+file.original_filename.downcase.sub(/[^\w\.\-]/, '_')
32
24
  path = File.join(settings[:path], filename)
33
25
  File.open(path, 'w') { |f| f.write(file.read) }
34
26
  filename
@@ -40,6 +32,14 @@ module Bhf
40
32
  end
41
33
  end
42
34
 
35
+ module ClassMethods
36
+ def setup_upload(settings)
37
+ self.bhf_upload_settings = settings.each_with_object([]) do |s, obj|
38
+ obj << {:path => '', :name => :file}.merge(s)
39
+ end
40
+ end
41
+ end
42
+
43
43
  end
44
44
  end
45
45
  end
data/lib/bhf/data.rb CHANGED
@@ -66,7 +66,7 @@ module Bhf
66
66
  private
67
67
 
68
68
  def supported_types(check_type)
69
- if [:boolean, :text].include?(check_type)
69
+ if [:boolean, :text, :array].include?(check_type)
70
70
  check_type
71
71
  elsif type_sym = group_types(check_type)
72
72
  type_sym
@@ -91,7 +91,7 @@ module Bhf
91
91
  def initialize(reflection, options = {})
92
92
  @reflection = reflection
93
93
  @info = options[:info]
94
- @link = options[:link].to_sym if options[:link]
94
+ @link = options[:link].downcase.to_sym unless options[:link].blank?
95
95
 
96
96
  @overwrite_type = options[:overwrite_type].to_sym if options[:overwrite_type]
97
97
  @overwrite_display_type = options[:overwrite_display_type].to_sym if options[:overwrite_display_type]
@@ -0,0 +1,138 @@
1
+ module Bhf
2
+ module Mongoid
3
+ module Document
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ class Field
8
+ attr_reader :type, :name
9
+
10
+ def initialize(mongoid_field)
11
+ @name = mongoid_field.name
12
+ @type = mongoid_field.type.to_s.downcase.to_sym
13
+ @type = :primary_key if @type == :'bson::objectid'
14
+ end
15
+ end
16
+
17
+ class Reflection
18
+ attr_reader :name, :macro, :klass, :primary_key_name
19
+
20
+ def initialize(mongoid_field)
21
+ @name = mongoid_field.name
22
+ @klass = mongoid_field.class_name.constantize
23
+ @primary_key_name = mongoid_field.key
24
+ @macro = case mongoid_field.macro
25
+ when :references_and_referenced_in_many
26
+ :has_and_belongs_to_many
27
+ when :references_many
28
+ :has_many
29
+ when :references_one
30
+ :has_one
31
+ when :referenced_in
32
+ :belongs_to
33
+ else
34
+ mongoid_field.macro
35
+ end
36
+ end
37
+ end
38
+
39
+ def to_bhf_s
40
+ return title if self.respond_to? :title
41
+ return name if self.respond_to? :name
42
+
43
+ if self.respond_to? :attributes
44
+ return title if attributes['title']
45
+ return name if attributes['name']
46
+ return "#{self.class.to_s.humanize} ID: #{send(self.class.primary_key)}" if attributes[self.class.primary_key]
47
+ end
48
+
49
+ self.to_s.humanize
50
+ end
51
+
52
+ module ClassMethods
53
+ def columns_hash
54
+ c = {}
55
+ fields.each_pair do |key, meta|
56
+ next if meta.options[:metadata]
57
+ next if key == '_type'
58
+ c[key] = Field.new(meta)
59
+ end
60
+ c
61
+ end
62
+
63
+ def reflections
64
+ c = {}
65
+ relations.each do |key, meta|
66
+ next if meta.macro == :embedded_in
67
+ c[key.to_sym] = Reflection.new(meta)
68
+ end
69
+ c
70
+ end
71
+
72
+ def bhf_primary_key
73
+ '_id'
74
+ end
75
+
76
+ def except(key)
77
+ if key == :order || key == :sort
78
+ #order_by.extras(:sort => []) #TODO: drop default_scope criteria
79
+ end
80
+ self
81
+ end
82
+
83
+ def order(a)
84
+ field, direction = a.split(' ')
85
+ return self if field.blank? or direction.blank?
86
+ self.send(direction.downcase, field)
87
+ end
88
+
89
+ def bhf_default_search(search_params)
90
+ return if (search_term = search_params[:text]).blank?
91
+
92
+ # TODO: add mongoid search
93
+ #return find(:all, :conditions => {:nick => /^antp/i})
94
+ return where("this.nick == 'antpaw'")
95
+ end
96
+
97
+ def get_embedded_parent(parent_id, &block)
98
+ relations.each do |key, meta|
99
+ next unless meta.macro == :embedded_in
100
+ parent = meta.class_name.constantize
101
+ parent = parent.find(parent_id) rescue nil
102
+
103
+ if parent
104
+ return parent unless block_given?
105
+ return block.call(parent, meta)
106
+ end
107
+ end
108
+ end
109
+
110
+ def bhf_new_embed(parent_id, params = nil)
111
+ get_embedded_parent parent_id do |parent, meta|
112
+ if parent.relations[meta.inverse_of.to_s].macro == :embeds_one
113
+ parent.send("build_#{meta.inverse_of}", params)
114
+ else
115
+ parent.send(meta.inverse_of).build(params)
116
+ end
117
+ end
118
+ end
119
+
120
+ def bhf_find_embed(parent_id, ref_id)
121
+ get_embedded_parent parent_id do |parent, meta|
122
+ relation = parent.send(meta.inverse_of)
123
+ if parent.relations[meta.inverse_of.to_s].macro == :embeds_one
124
+ relation
125
+ else
126
+ relation.find(ref_id)
127
+ end
128
+ end
129
+ end
130
+
131
+ def bhf_embedded?
132
+ embedded?
133
+ end
134
+ end
135
+
136
+ end
137
+ end
138
+ end
@@ -1,3 +1,31 @@
1
+ module Kaminari
2
+ module Helpers
3
+ class Tag
4
+ def initialize(template, options = {}) #:nodoc:
5
+ @template, @options = template, options.dup
6
+ @param_name = @options.delete(:param_name)
7
+ @theme = @options[:theme] ? "#{@options.delete(:theme)}/" : ''
8
+ @params = @options[:params] ? template.params.merge(@options.delete :params) : template.params
9
+ end
10
+
11
+ def to_s(locals = {}) #:nodoc:
12
+ @template.render :partial => "kaminari/#{@theme}#{self.class.name.demodulize.underscore}", :locals => @options.merge(locals)
13
+ end
14
+
15
+ def page_url_for(page)
16
+ if @param_name.is_a?(Array)
17
+ @params[@param_name[0]] = (@params[@param_name[0]] || {}).merge(@param_name[1] => (page <= 1 ? nil : page))
18
+ return @template.url_for @params
19
+ end
20
+
21
+ @template.url_for @params.merge(@param_name => (page <= 1 ? nil : page))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ # TODO: lets hope they fix nested params and we can remove this
27
+
28
+
1
29
  module Bhf
2
30
 
3
31
  class Pagination
@@ -13,15 +41,14 @@ module Bhf
13
41
  def create(platform)
14
42
  platform_params = template.params[platform.name] || {}
15
43
 
16
- if page_links = template.will_paginate(platform.objects, {
17
- :previous_label => I18n.t('bhf.pagination.previous_label'),
18
- :next_label => I18n.t('bhf.pagination.next_label'),
19
- :renderer => LinkRenderer.new(self, platform),
20
- :container => false
21
- })
22
- links = "#{load_more(platform)} #{page_links}"
23
- elsif platform.objects.total_pages == 1 && platform.objects.size + @offset_to_add > @offset_per_page
24
- links = load_less(platform)
44
+ links = if !(page_links = template.paginate(platform.objects, {
45
+ :theme => 'bhf',
46
+ :param_name => [platform.name, 'page'],
47
+ :params => template.params
48
+ })).blank?
49
+ "#{load_more(platform)} #{page_links}"
50
+ elsif platform.objects.num_pages == 1 && platform.objects.size > @offset_to_add
51
+ load_less(platform)
25
52
  end
26
53
 
27
54
  if links
@@ -31,21 +58,17 @@ module Bhf
31
58
 
32
59
  def info(platform, options = {})
33
60
  collection = platform.objects
34
-
35
- unless collection.respond_to?(:total_pages)
36
- collection = collection.paginate({:page => 1, :per_page => collection.count+1})
37
- end
38
61
 
39
- if collection.total_pages > 1
62
+ if collection.respond_to?(:num_pages) and collection.num_pages > 1
40
63
  I18n.t('bhf.pagination.info.default', {
41
64
  :name => platform.title,
42
- :count => collection.total_entries,
43
- :offset_start => collection.offset + 1,
44
- :offset_end => collection.offset + collection.length
65
+ :count => collection.total_count,
66
+ :offset_start => collection.offset_value + 1,
67
+ :offset_end => collection.offset_value + collection.length
45
68
  })
46
69
  else
47
70
  I18n.t('bhf.pagination.info', {
48
- :name_zero => platform.title_zero,
71
+ :name_zero => platform.title,
49
72
  :name_singular => platform.title_singular,
50
73
  :name_plural => platform.title,
51
74
  :count => collection.size
@@ -79,29 +102,6 @@ module Bhf
79
102
  def load_less(platform, attributes = {})
80
103
  load_more(platform, attributes, false)
81
104
  end
82
-
83
-
84
- class LinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
85
-
86
- def initialize(bhf_pagination, platform)
87
- @b_p = bhf_pagination
88
- @platform = platform
89
- end
90
-
91
- def link(text, page, attributes = {})
92
- platform_params = @b_p.template.params[@platform.name] || {}
93
- platform_params[:page] = page
94
-
95
- @b_p.template.link_to(
96
- text,
97
- @b_p.template.bhf_page_path(
98
- @platform.page_name,
99
- @b_p.template.params.merge(@platform.name => platform_params)
100
- ), {:class => 'page_number'}.merge(attributes)
101
- )
102
- end
103
-
104
- end
105
105
 
106
106
  end
107
- end
107
+ end
data/lib/bhf/platform.rb CHANGED
@@ -66,14 +66,13 @@ module Bhf
66
66
  chain = do_search(chain, options[:search])
67
67
  end
68
68
 
69
- if paginate_options && !sortable
70
- chain = chain.paginate(paginate_options)
71
- end
72
-
73
- if chain == model
74
- chain = chain.all
69
+
70
+ chain = if paginate_options && !sortable
71
+ chain.page(paginate_options[:page]).per(paginate_options[:per_page])
72
+ elsif chain == model
73
+ chain.all
75
74
  end
76
-
75
+
77
76
  @objects = chain
78
77
  end
79
78
 
@@ -172,7 +171,7 @@ module Bhf
172
171
  :overwrite_type => form_options(:types, name),
173
172
  :overwrite_display_type => table_options(:types, name),
174
173
  :info => I18n.t("bhf.platforms.#{@name}.infos.#{name}", :default => '')
175
- }, model.primary_key)
174
+ }, model.bhf_primary_key)
176
175
  end
177
176
 
178
177
  model.reflections.each_pair do |name, props|
@@ -184,7 +183,7 @@ module Bhf
184
183
  })
185
184
 
186
185
  fk = all[name.to_s].reflection.primary_key_name
187
- if all.has_key?(fk)
186
+ if all.has_key?(fk) and fk != name.to_s
188
187
  all.delete(fk)
189
188
  end
190
189
  end
data/lib/engine.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'haml'
2
- require 'will_paginate'
3
- require 'will_paginate/view_helpers/action_view'
2
+ require 'kaminari'
4
3
 
5
4
  module Bhf
6
5
  class Engine < Rails::Engine