bhf 0.1.6 → 0.2.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.
Files changed (34) hide show
  1. data/app/controllers/bhf/application_controller.rb +50 -29
  2. data/app/controllers/bhf/entries_controller.rb +7 -7
  3. data/app/controllers/bhf/pages_controller.rb +19 -15
  4. data/app/helpers/bhf/entries_helper.rb +1 -1
  5. data/app/helpers/bhf/pages_helper.rb +4 -4
  6. data/app/views/bhf/_footer.haml +11 -1
  7. data/app/views/bhf/application/index.haml +1 -1
  8. data/app/views/bhf/entries/_form.haml +4 -2
  9. data/app/views/bhf/entries/form/belongs_to/_account_scope.haml +2 -0
  10. data/app/views/bhf/entries/form/belongs_to/_radio.haml +2 -1
  11. data/app/views/bhf/entries/form/belongs_to/_select.haml +2 -1
  12. data/app/views/bhf/entries/form/belongs_to/_static.haml +1 -1
  13. data/app/views/bhf/entries/form/column/_password.haml +2 -0
  14. data/app/views/bhf/entries/form/column/_static.haml +2 -2
  15. data/app/views/bhf/entries/form/has_and_belongs_to_many/_account_scope.haml +2 -0
  16. data/app/views/bhf/entries/form/has_and_belongs_to_many/_check_box.haml +2 -1
  17. data/app/views/bhf/entries/form/has_and_belongs_to_many/_static.haml +1 -1
  18. data/app/views/bhf/entries/form/has_many/_static.haml +1 -1
  19. data/app/views/bhf/entries/form/has_one/_account_scope.haml +3 -0
  20. data/app/views/bhf/entries/form/has_one/_static.haml +1 -1
  21. data/app/views/bhf/pages/_platform.haml +10 -10
  22. data/app/views/layouts/bhf/{default.haml → application.haml} +8 -6
  23. data/config/locales/en.yml +1 -0
  24. data/lib/bhf/active_record.rb +30 -9
  25. data/lib/bhf/data.rb +27 -10
  26. data/lib/bhf/pagination.rb +10 -11
  27. data/lib/bhf/platform.rb +62 -41
  28. data/lib/bhf/settings.rb +8 -9
  29. data/lib/bhf.rb +2 -1
  30. data/lib/engine.rb +3 -1
  31. data/lib/rails/generators/bhf/templates/initializer.rb +4 -3
  32. data/public/javascripts/bhf.js +1 -1
  33. data/public/stylesheets/bhf.css +1 -1
  34. metadata +24 -14
@@ -1,51 +1,72 @@
1
1
  class Bhf::ApplicationController < ActionController::Base
2
-
2
+
3
3
  protect_from_forgery
4
-
5
- before_filter :init_time, :check_admin_account, :load_config, :set_title
6
4
 
7
- helper_method :entry_path, :new_entry_path, :entries_path, :edit_entry_path
8
- layout 'bhf/default'
5
+ before_filter :init_time, :check_admin_account, :setup_current_account, :load_config, :set_title
6
+
7
+ helper_method :current_account
8
+ layout 'bhf/application'
9
9
 
10
10
  def index
11
11
 
12
12
  end
13
13
 
14
-
15
14
  private
16
15
 
17
16
  def check_admin_account
18
- auth_logic = Bhf::Engine.config.auth_logic_from.constantize.new
19
-
20
- if auth_logic.respond_to?(:current_admin_account) && auth_logic.current_admin_account
17
+ if session[Bhf::Engine.config.session_auth_name.to_s] == true
21
18
  return true
22
- else
23
- redirect_to(root_url, :error => t('bhf.helpers.login.error')) and return false
24
19
  end
25
- end
26
20
 
27
- def load_config
28
- @config = Bhf::Settings.new(
29
- YAML::load(IO.read('config/bhf.yml'))
30
- # Bhf::Engine.config.bhf_logic
31
- )
21
+ redirect_to(root_url, :error => t('bhf.helpers.login.error')) and return false
32
22
  end
33
23
 
34
-
35
- def new_entry_path(platform, extra_params = {})
36
- new_bhf_entry_path platform, extra_params
24
+ def setup_current_account
25
+ if Bhf::Engine.config.current_admin_account
26
+ # TODO: wtf ActionDispatch::Cookies::CookieOverflow
27
+ account = session[Bhf::Engine.config.current_admin_account.to_s]
28
+ @current_account = account.class.find(account.id)
29
+ end
37
30
  end
38
-
39
- def entries_path(platform, extra_params = {})
40
- bhf_entries_path platform, extra_params
31
+
32
+ def current_account
33
+ @current_account
41
34
  end
42
35
 
43
- def entry_path(platform, object, extra_params = {})
44
- bhf_entry_path platform, object, extra_params
36
+ def load_config
37
+ @config = Bhf::Settings.new(roles_yml(get_account_roles))
45
38
  end
46
-
47
- def edit_entry_path(platform, object, extra_params = {})
48
- edit_bhf_entry_path platform, object, extra_params
39
+
40
+ def roles_yml(roles = nil)
41
+ if roles.is_a?(String)
42
+ load_yml("/#{roles}")
43
+ elsif roles.is_a?(Array)
44
+ files = roles.each_with_object({'pages' => []}) do |r, account_roles|
45
+ pages = load_yml("/#{r}")['pages']
46
+ account_roles['pages'] += pages if pages
47
+ end
48
+ # TODO: merge platforms of the same pages rather the replace them
49
+ files['pages'].uniq! do |a|
50
+ a.keys
51
+ end
52
+ files
53
+ else
54
+ load_yml
55
+ end
56
+ end
57
+
58
+ def load_yml(suffix = nil)
59
+ YAML::load(IO.read("config/bhf#{suffix}.yml"))
60
+ end
61
+
62
+ def get_account_roles
63
+ return unless current_account
64
+
65
+ if current_account.respond_to?(:role)
66
+ current_account.role.is_a?(String) ? current_account.role : current_account.role.to_bhf_s
67
+ elsif current_account.respond_to?(:roles)
68
+ current_account.roles.collect(&:to_bhf_s)
69
+ end
49
70
  end
50
71
 
51
72
 
@@ -74,4 +95,4 @@ class Bhf::ApplicationController < ActionController::Base
74
95
  session[:return_to] = nil
75
96
  end
76
97
 
77
- end
98
+ end
@@ -6,11 +6,11 @@ class Bhf::EntriesController < Bhf::ApplicationController
6
6
  @object = @model.new
7
7
  after_load
8
8
 
9
- @form_url = entries_path(@platform.name)
9
+ @form_url = bhf_entries_path(@platform.name)
10
10
  end
11
11
 
12
12
  def edit
13
- @form_url = entry_path(@platform.name, @object)
13
+ @form_url = bhf_entry_path(@platform.name, @object)
14
14
 
15
15
  if @quick_edit
16
16
  render :layout => 'bhf/quick_edit'
@@ -26,9 +26,9 @@ class Bhf::EntriesController < Bhf::ApplicationController
26
26
  manage_many_to_many
27
27
  after_save
28
28
 
29
- redirect_back_or_default(entry_path(@platform.name, @object), :notice => set_message('create.success', @model))
29
+ redirect_back_or_default(bhf_entry_path(@platform.name, @object), :notice => set_message('create.success', @model))
30
30
  else
31
- @form_url = entries_path(@platform.name)
31
+ @form_url = bhf_entries_path(@platform.name)
32
32
  render :new
33
33
  end
34
34
  end
@@ -43,10 +43,10 @@ class Bhf::EntriesController < Bhf::ApplicationController
43
43
  if @quick_edit
44
44
  render :json => object_to_bhf_display_hash, :status => :ok
45
45
  else
46
- redirect_back_or_default(entry_path(@platform.name, @object), :notice => set_message('update.success', @model))
46
+ redirect_back_or_default(bhf_entry_path(@platform.name, @object), :notice => set_message('update.success', @model))
47
47
  end
48
48
  else
49
- @form_url = entry_path(@platform.name, @object)
49
+ @form_url = bhf_entry_path(@platform.name, @object)
50
50
 
51
51
  r_settings = {:status => :unprocessable_entity}
52
52
  r_settings[:layout] = 'bhf/quick_edit' if @quick_edit
@@ -71,7 +71,7 @@ class Bhf::EntriesController < Bhf::ApplicationController
71
71
  end
72
72
 
73
73
  def load_platform
74
- @platform = @config.find_platform(params[:platform])
74
+ @platform = @config.find_platform(params[:platform], current_account)
75
75
  end
76
76
 
77
77
  def load_model
@@ -6,9 +6,6 @@ class Bhf::PagesController < Bhf::ApplicationController
6
6
  raise Exception.new("Page '#{@page}' could not be found")
7
7
  end
8
8
 
9
- # TODO: page offset form bhf.yml
10
- @pagination = Bhf::Pagination.new(10)
11
-
12
9
  if request.xhr?
13
10
  params.each do |key, value|
14
11
  return render_platform(key) if value.is_a?(Hash)
@@ -16,7 +13,8 @@ class Bhf::PagesController < Bhf::ApplicationController
16
13
  end
17
14
 
18
15
  @platforms = platform_options.each_with_object([]) do |opts, obj|
19
- platform = Bhf::Platform.new(opts, @page)
16
+ platform = Bhf::Platform.new(opts, @page, current_account)
17
+ platform.pagination = Bhf::Pagination.new(platform.entries_per_page)
20
18
  platform.paginated_objects = paginate_platform_objects(platform)
21
19
  obj << platform
22
20
  end
@@ -24,29 +22,35 @@ class Bhf::PagesController < Bhf::ApplicationController
24
22
 
25
23
  private
26
24
 
25
+ def set_page
26
+ @page = params[:page]
27
+ end
28
+
27
29
  def render_platform(platform_name)
28
- @platform = @config.find_platform(platform_name)
30
+ @platform = @config.find_platform(platform_name, current_account)
29
31
 
32
+ @platform.pagination = Bhf::Pagination.new(@platform.entries_per_page)
30
33
  @platform.paginated_objects = paginate_platform_objects(@platform)
31
34
 
32
35
  render '_platform', :layout => false
33
36
  end
34
37
 
35
- def set_page
36
- @page = params[:page]
37
- end
38
-
39
38
  def check_params(platform)
40
39
  page = 1
41
- page = params[platform][:page].to_i if params[platform] && !params[platform][:page].blank?
42
- per_page = @pagination.offset_per_page
43
- per_page = params[platform][:per_page].to_i if params[platform] && !params[platform][:per_page].blank?
40
+ if params[platform.name] && !params[platform.name][:page].blank?
41
+ page = params[platform.name][:page].to_i
42
+ end
43
+
44
+ per_page = platform.pagination.offset_per_page
45
+ if params[platform.name] && !params[platform.name][:per_page].blank?
46
+ per_page = params[platform.name][:per_page].to_i
47
+ end
44
48
 
45
49
  return :page => page, :per_page => per_page
46
50
  end
47
-
51
+
48
52
  def paginate_platform_objects(platform)
49
- platform.prepare_objects(params[platform.name] || {}).paginate(check_params(platform.name))
53
+ platform.prepare_objects(params[platform.name] || {}, check_params(platform))
50
54
  end
51
55
 
52
- end
56
+ end
@@ -6,7 +6,7 @@ module Bhf
6
6
  end
7
7
 
8
8
  def reflection_node(f, field, &block)
9
- return if field.form_type === :static && f.object.new_record? && f.object.send(field.reflection.name).blank?
9
+ return if field.form_type == :static && f.object.new_record? && f.object.send(field.reflection.name).blank?
10
10
  render :partial => 'bhf/helper/reflection_node', :locals => {
11
11
  :f => f, :field => field, :input => with_output_buffer(&block)
12
12
  }
@@ -2,13 +2,13 @@ module Bhf
2
2
  module PagesHelper
3
3
 
4
4
  def get_value(key, platform_name)
5
- params[platform_name][key] if params[platform_name] && params[platform_name][key]
5
+ params[platform_name][key] if params[platform_name] && params[platform_name][key] && params[platform_name][key].is_a?(String)
6
6
  end
7
7
 
8
8
  def current_order_path(order_by, platform_name)
9
9
  params_platfrom = params[platform_name] ? params[platform_name].clone : {}
10
10
 
11
- if params_platfrom['order'] === order_by && params_platfrom['direction'] != 'desc'
11
+ if params_platfrom['order'] == order_by && params_platfrom['direction'] != 'desc'
12
12
  params_platfrom['direction'] = 'desc'
13
13
  else
14
14
  params_platfrom['direction'] = 'asc'
@@ -21,9 +21,9 @@ module Bhf
21
21
 
22
22
  def order_class(order_by, platform_name)
23
23
  params_platfrom = params[platform_name] ? params[platform_name] : {}
24
- return unless params_platfrom['order'] === order_by
24
+ return unless params_platfrom['order'] == order_by
25
25
 
26
- params_platfrom['direction'] === 'desc' ? 'sorted desc' : 'sorted asc'
26
+ params_platfrom['direction'] == 'desc' ? 'sorted desc' : 'sorted asc'
27
27
  end
28
28
 
29
29
  end
@@ -1,4 +1,13 @@
1
- %p.stats= "Page rendered in ~#{(Time.now-@start_time).round(2)} seconds"
1
+ - if defined?(current_account)
2
+ -# TODO: i18n and custom logout path
3
+ %p.user
4
+ You are logged in as
5
+ %strong= current_account.name
6
+ = link_to 'Logout', logout_path, :method => :delete
7
+
8
+ - if @start_time
9
+ %p.stats= "Page rendered in ~#{(Time.now-@start_time).round(2)} seconds"
10
+
2
11
  %p.copy
3
12
  Made by
4
13
  = link_to '@antpaw', 'http://twitter.com/antpaw'
@@ -6,6 +15,7 @@
6
15
  = link_to 'Rails 3', 'http://twitter.com/antpaw'
7
16
  and
8
17
  = link_to 'MooTools', 'http://mootools.net/'
18
+
9
19
  %p.info
10
20
  More information on
11
21
  = link_to 'GitHub', 'http://github.com/antpaw/bahnhof/'
@@ -1,3 +1,3 @@
1
1
  %ul
2
2
  - @config.pages.each do |page|
3
- %li= link_to page, bhf_page_path(page)
3
+ %li= link_to t("bhf.pages.navigation.#{page}", :default => page.capitalize), bhf_page_path(page)
@@ -5,10 +5,12 @@
5
5
  = render :partial => 'validation_errors', :locals => {:f => f}
6
6
 
7
7
  - @platform.fields.each do |field|
8
- - unless field.form_type === :hidden
8
+ - unless field.form_type == :hidden
9
9
  = render :partial => "bhf/entries/form/#{field.macro}/#{field.form_type}", :locals => {:f => f, :field => field}
10
10
 
11
11
  - unless @quick_edit
12
12
  .node
13
13
  .label
14
- .input= f.submit f.object.new_record? ? new_t(@platform) : edit_t(@platform)
14
+ .input
15
+ = f.submit f.object.new_record? ? new_t(@platform) : edit_t(@platform)
16
+ = link_to t('bhf.helpers.entry.cancel'), bhf_page_path(@platform.page_name), :class => 'cancel'
@@ -0,0 +1,2 @@
1
+ - data_source = current_account.send(field.reflection.name.to_s.pluralize)
2
+ = render :partial => 'bhf/entries/form/belongs_to/select', :locals => {:data_source => data_source, :field => field, :f => f}
@@ -1,7 +1,8 @@
1
+ - data_source ||= field.reflection.klass.all
1
2
  = reflection_node f, field do
2
3
  - fk = field.reflection.primary_key_name
3
4
  %ul
4
- - field.reflection.klass.all.each do |obj|
5
+ - data_source.each do |obj|
5
6
  %li
6
7
  = f.radio_button fk, obj.id, :id => "belongs_to_#{fk}_#{obj.id}"
7
8
  = label_tag "belongs_to_#{fk}_#{obj.id}", obj.to_bhf_s
@@ -1,6 +1,7 @@
1
+ - data_source ||= field.reflection.klass.all
1
2
  - fk = field.reflection.primary_key_name
2
3
  .node
3
4
  .label= f.label fk
4
5
  .input
5
- = f.select fk, options_from_collection_for_select(field.reflection.klass.all, :id, :to_bhf_s, f.object.send(fk)), :include_blank => true
6
+ = f.select fk, options_from_collection_for_select(data_source, :id, :to_bhf_s, f.object.send(fk)), :include_blank => true
6
7
  = render :partial => 'bhf/helper/field_errors', :locals => {:f => f, :field => fk}
@@ -4,6 +4,6 @@
4
4
  = t 'bhf.helpers.entry.empty'
5
5
  - else
6
6
  - if field.link
7
- = link_to ref_object.to_bhf_s, edit_entry_path(field.link, ref_object), :class => 'quick_edit'
7
+ = link_to ref_object.to_bhf_s, edit_bhf_entry_path(field.link, ref_object), :class => 'quick_edit'
8
8
  - else
9
9
  = ref_object.to_bhf_s
@@ -0,0 +1,2 @@
1
+ = node f, field do
2
+ = f.password_field field.name
@@ -1,8 +1,8 @@
1
1
  - if value = f.object.send(field.name)
2
2
  = node f, field do
3
- - if field.display_type === :primary_key
3
+ - if field.display_type == :primary_key
4
4
  %strong= value
5
- - elsif field.display_type === :date
5
+ - elsif field.display_type == :date
6
6
  = l value, :format => :long
7
7
  - else
8
8
  = value
@@ -0,0 +1,2 @@
1
+ - data_source = current_account.send(field.reflection.name.to_s.pluralize)
2
+ = render :partial => 'bhf/entries/form/has_and_belongs_to_many/check_box', :locals => {:data_source => data_source, :field => field, :f => f}
@@ -1,4 +1,5 @@
1
+ - data_source ||= field.reflection.klass.all
1
2
  = reflection_node f, field do
2
3
  %ul
3
- - field.reflection.klass.all.each do |obj|
4
+ - data_source.each do |obj|
4
5
  %li= f.many_to_many_check_box obj, field.reflection.name, params
@@ -7,6 +7,6 @@
7
7
  - ref_objects.each do |ref_object|
8
8
  %li
9
9
  - if field.link
10
- = link_to ref_object.to_bhf_s, edit_entry_path(field.link, ref_object), :class => 'quick_edit'
10
+ = link_to ref_object.to_bhf_s, edit_bhf_entry_path(field.link, ref_object), :class => 'quick_edit'
11
11
  - else
12
12
  = ref_object.to_bhf_s
@@ -7,6 +7,6 @@
7
7
  - ref_objects.each do |ref_object|
8
8
  %li
9
9
  - if field.link
10
- = link_to ref_object.to_bhf_s, edit_entry_path(field.link, ref_object), :class => 'quick_edit'
10
+ = link_to ref_object.to_bhf_s, edit_bhf_entry_path(field.link, ref_object), :class => 'quick_edit'
11
11
  - else
12
12
  = ref_object.to_bhf_s
@@ -0,0 +1,3 @@
1
+ - # TODO: this doesnt work
2
+ - data_source = current_account.send(field.reflection.name.to_s.pluralize)
3
+ = render :partial => 'bhf/entries/form/belongs_to/select', :locals => {:data_source => data_source, :field => field, :f => f}
@@ -4,6 +4,6 @@
4
4
  = t 'bhf.helpers.entry.empty'
5
5
  - else
6
6
  - if field.link
7
- = link_to ref_object.to_bhf_s, edit_entry_path(field.link, ref_object), :class => 'quick_edit'
7
+ = link_to ref_object.to_bhf_s, edit_bhf_entry_path(field.link, ref_object), :class => 'quick_edit'
8
8
  - else
9
9
  = ref_object.to_bhf_s
@@ -1,8 +1,8 @@
1
- - @pagination.template = self
1
+ - @platform.pagination.template = self
2
2
  %table{:class => (@platform.custom_columns? ? 'custom_columns' : 'default_columns')}
3
3
  %caption
4
4
  %h4= @platform.title
5
- %p.info= @pagination.info @platform, :entry_name => 'entry'
5
+ %p.info= @platform.pagination.info @platform, :entry_name => 'entry'
6
6
 
7
7
  - if @platform.search?
8
8
  = form_for @platform.name, :html => {:method => 'get', :class => 'search'} do |f|
@@ -11,7 +11,7 @@
11
11
  = f.hidden_field :order, :value => get_value(:order, @platform.name)
12
12
  = f.hidden_field :direction, :value => get_value(:direction, @platform.name)
13
13
 
14
- %p.create= link_to new_t(@platform), new_entry_path(@platform.name), :class => 'alt_button'
14
+ %p.create= link_to new_t(@platform), new_bhf_entry_path(@platform.name), :class => 'alt_button'
15
15
 
16
16
  %thead
17
17
  %tr
@@ -25,22 +25,22 @@
25
25
  %tfoot
26
26
  %tr
27
27
  %td{:colspan => @platform.columns.count+1}
28
- = @pagination.paginate @platform
28
+ = @platform.pagination.paginate @platform
29
29
  %tbody
30
30
  - if @platform.paginated_objects.any?
31
31
  - @platform.paginated_objects.each do |object|
32
- - edit_link = edit_entry_path(@platform.name, object)
32
+ - edit_link = edit_bhf_entry_path(@platform.name, object)
33
33
  %tr
34
34
  - @platform.columns.each do |column|
35
35
  %td{:class => "#{column.field.macro} #{column.field.display_type}", :'data-column-name' => column.name}
36
36
  - unless column.field.macro == :column && object.send(column.name).blank?
37
- = link_to edit_link, :class => 'quick_edit' do
38
- - p = "bhf/pages/macro/#{column.field.macro}/#{column.field.display_type}"
39
- = render :partial => p, :locals => {:column => column, :object => object}
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')
40
40
  %td.action
41
41
  = link_to edit_t(@platform), edit_link, :class => 'edit'
42
- = link_to delete_t(@platform), entry_path(@platform.name, object), :method => :delete, :confirm => t('bhf.helpers.promts.confirm'), :remote => true, :class => 'delete'
42
+ = link_to delete_t(@platform), bhf_entry_path(@platform.name, object), :method => :delete, :confirm => t('bhf.helpers.promts.confirm'), :class => 'delete'
43
43
  - else
44
44
  %td.no_entries{:colspan => @platform.columns.count+1}
45
45
  = t 'bhf.pagination.info.nothing_found', :name => t('bhf.pagination.entry').pluralize
46
- = link_to new_t(@platform), new_entry_path(@platform.name)
46
+ = link_to new_t(@platform), new_bhf_entry_path(@platform.name)
@@ -10,13 +10,15 @@
10
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'
11
11
  %body
12
12
  %header
13
+ -# TODO: logo as css bg for better cache and no jumping?
13
14
  %h1= image_tag 'logo_bhf.png'
14
- %nav
15
- %ul
16
- - @config.pages.each do |page|
17
- %li{:class => ('active' if @page == page)}
18
- = link_to t("bhf.pages.navigation.#{page}", :default => page), bhf_page_path(page)
19
-
15
+ - if @config && @config.pages.any?
16
+ %nav
17
+ %ul
18
+ - @config.pages.each do |page|
19
+ %li{:class => ('active' if @page == page)}
20
+ = link_to t("bhf.pages.navigation.#{page}", :default => page.capitalize), bhf_page_path(page)
21
+
20
22
  #content
21
23
  #main
22
24
  = render :partial => 'bhf/helper/flash', :locals => {:flash => flash}
@@ -13,6 +13,7 @@ en:
13
13
  edit: "Edit %{platform_name}"
14
14
  delete: "Delete %{platform_name}"
15
15
  empty: Empty
16
+ cancel: Cancel
16
17
  entries:
17
18
  empty: Empty
18
19
  login:
@@ -1,18 +1,39 @@
1
1
  module Bhf
2
2
  module ActiveRecord
3
3
 
4
- def to_bhf_s
5
- return title if self.respond_to? :title
6
- return name if self.respond_to? :name
4
+ module Object
5
+ def to_bhf_s
6
+ return title if self.respond_to? :title
7
+ return name if self.respond_to? :name
7
8
 
8
- if self.respond_to? :attributes
9
- return title if attributes['title']
10
- return name if attributes['name']
11
- return "#{self.class.to_s.humanize} ID: #{send(self.class.primary_key)}" if attributes[self.class.primary_key]
12
- end
9
+ if self.respond_to? :attributes
10
+ return title if attributes['title']
11
+ return name if attributes['name']
12
+ return "#{self.class.to_s.humanize} ID: #{send(self.class.primary_key)}" if attributes[self.class.primary_key]
13
+ end
13
14
 
14
- self.to_s.humanize
15
+ self.to_s.humanize
16
+ end
15
17
  end
16
18
 
19
+ module Self
20
+ def bhf_default_search(search_term)
21
+ where_statement = []
22
+ columns_hash.each_pair do |name, props|
23
+ is_number = search_term.to_i.to_s == search_term || search_term.to_f.to_s == search_term
24
+
25
+ if props.type == :string || props.type == :text
26
+ where_statement << "#{name} LIKE '%#{search_term}%'"
27
+ elsif props.type == :integer && is_number
28
+ where_statement << "#{name} = #{search_term.to_i}"
29
+ elsif props.type == :float && is_number
30
+ where_statement << "#{name} = #{search_term.to_f}"
31
+ end
32
+ end
33
+
34
+ where_statement.join(' OR ')
35
+ end
36
+ end
37
+
17
38
  end
18
39
  end
data/lib/bhf/data.rb CHANGED
@@ -1,15 +1,28 @@
1
1
  module Bhf
2
2
  module Data
3
+ class AbstractField
4
+ attr_reader :name, :form_type, :info, :macro, :display_type, :overwrite_display_type
5
+
6
+ def initialize(props)
7
+ @name = props[:name]
8
+ @form_type = props[:type]
9
+ @display_type = props[:type]
10
+ @overwrite_display_type = props[:type]
11
+ @info = props[:info]
12
+ @macro = :column
13
+ end
14
+ end
3
15
 
4
16
  class Field
5
17
 
6
- attr_reader :info
18
+ attr_reader :info, :overwrite_display_type
7
19
 
8
20
  def initialize(props, options = {}, pk = 'id')
9
21
  @props = props
10
- @info = options[:info] if options[:info]
22
+ @info = options[:info] if options[:info] # TODO: i think this is wrong because info is atleast ''
11
23
 
12
24
  @overwrite_type = options[:overwrite_type].to_sym if options[:overwrite_type]
25
+ @overwrite_display_type = options[:overwrite_display_type].to_sym if options[:overwrite_display_type]
13
26
 
14
27
  @primary_key = pk
15
28
  end
@@ -27,7 +40,7 @@ module Bhf
27
40
  def form_type
28
41
  return @overwrite_type if @overwrite_type
29
42
 
30
- if name === @primary_key || name === 'updated_at' || name === 'created_at'
43
+ if name == @primary_key || name == 'updated_at' || name == 'created_at'
31
44
  :static
32
45
  else
33
46
  supported_types(@props.type)
@@ -35,9 +48,11 @@ module Bhf
35
48
  end
36
49
 
37
50
  def display_type
38
- if name === @primary_key
51
+ return @overwrite_display_type if @overwrite_display_type
52
+
53
+ if name == @primary_key
39
54
  :primary_key
40
- elsif name === 'updated_at' || name === 'created_at'
55
+ elsif name == 'updated_at' || name == 'created_at'
41
56
  :date
42
57
  else
43
58
  supported_types(@props.type)
@@ -63,7 +78,7 @@ module Bhf
63
78
  def group_types(type_sym)
64
79
  return :date if [:date, :datetime, :timestamp, :time, :year].include?(type_sym)
65
80
  return :number if [:integer, :float].include?(type_sym)
66
- return :file if type_sym === :file
81
+ return :file if type_sym == :file
67
82
  end
68
83
 
69
84
  end
@@ -71,7 +86,7 @@ module Bhf
71
86
 
72
87
  class Reflection
73
88
 
74
- attr_reader :reflection, :info, :link
89
+ attr_reader :reflection, :info, :link, :overwrite_display_type
75
90
 
76
91
  def initialize(reflection, options = {})
77
92
  @reflection = reflection
@@ -79,6 +94,7 @@ module Bhf
79
94
  @link = options[:link].to_sym if options[:link]
80
95
 
81
96
  @overwrite_type = options[:overwrite_type].to_sym if options[:overwrite_type]
97
+ @overwrite_display_type = options[:overwrite_display_type].to_sym if options[:overwrite_display_type]
82
98
  end
83
99
 
84
100
  def macro
@@ -88,9 +104,9 @@ module Bhf
88
104
  def type
89
105
  return @overwrite_type if @overwrite_type
90
106
 
91
- if macro === :has_and_belongs_to_many
107
+ if macro == :has_and_belongs_to_many
92
108
  :check_box
93
- elsif macro === :belongs_to
109
+ elsif macro == :belongs_to
94
110
  :select
95
111
  else
96
112
  :static
@@ -102,6 +118,7 @@ module Bhf
102
118
  end
103
119
 
104
120
  def display_type
121
+ return @overwrite_display_type if @overwrite_display_type
105
122
  :default
106
123
  end
107
124
 
@@ -114,7 +131,7 @@ module Bhf
114
131
 
115
132
  class Column
116
133
 
117
- attr_reader :name, :field
134
+ attr_reader :name, :field, :overwrite_display_type
118
135
 
119
136
  def initialize(field)
120
137
  @name = field.name