ui_bibz 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/.travis.yml +13 -0
  4. data/Gemfile +15 -0
  5. data/Gemfile.lock +171 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +728 -0
  8. data/README.rdoc +0 -0
  9. data/Rakefile +34 -0
  10. data/config/locales/en.yml +14 -0
  11. data/config/locales/fr.yml +14 -0
  12. data/doc/images/alert.png +0 -0
  13. data/doc/images/breadcrumb.png +0 -0
  14. data/doc/images/button.png +0 -0
  15. data/doc/images/button_dropdown.png +0 -0
  16. data/doc/images/button_group.png +0 -0
  17. data/doc/images/button_link.png +0 -0
  18. data/doc/images/button_split_dropdown.png +0 -0
  19. data/doc/images/glyph.png +0 -0
  20. data/doc/images/list.png +0 -0
  21. data/doc/images/nav.png +0 -0
  22. data/doc/images/panel.png +0 -0
  23. data/doc/images/table.png +0 -0
  24. data/doc/images/table_pagination_per_page.png +0 -0
  25. data/doc/images/table_panel.png +0 -0
  26. data/doc/images/table_search_field.png +0 -0
  27. data/doc/images/ui-bibz-logo-without-border.gif +0 -0
  28. data/doc/images/ui-bibz-logo.gif +0 -0
  29. data/lib/tasks/ui_bibz_tasks.rake +4 -0
  30. data/lib/ui_bibz/concerns/models/searchable.rb +93 -0
  31. data/lib/ui_bibz/helpers/meta_helper.rb +27 -0
  32. data/lib/ui_bibz/helpers/ui_helper.rb +108 -0
  33. data/lib/ui_bibz/helpers/utils_helper.rb +9 -0
  34. data/lib/ui_bibz/rails/engine.rb +39 -0
  35. data/lib/ui_bibz/ui/alert.rb +67 -0
  36. data/lib/ui_bibz/ui/breadcrumb/breadcrumb.rb +54 -0
  37. data/lib/ui_bibz/ui/breadcrumb/components/breadcrumb_link.rb +62 -0
  38. data/lib/ui_bibz/ui/button/button.rb +65 -0
  39. data/lib/ui_bibz/ui/button/button_dropdown.rb +56 -0
  40. data/lib/ui_bibz/ui/button/button_group.rb +62 -0
  41. data/lib/ui_bibz/ui/button/button_link.rb +66 -0
  42. data/lib/ui_bibz/ui/button/button_split_dropdown.rb +80 -0
  43. data/lib/ui_bibz/ui/component.rb +137 -0
  44. data/lib/ui_bibz/ui/dropdown/components/dropdown_list.rb +73 -0
  45. data/lib/ui_bibz/ui/dropdown/dropdown.rb +101 -0
  46. data/lib/ui_bibz/ui/glyph.rb +92 -0
  47. data/lib/ui_bibz/ui/grid/components/col.rb +74 -0
  48. data/lib/ui_bibz/ui/grid/components/row.rb +32 -0
  49. data/lib/ui_bibz/ui/grid/grid.rb +108 -0
  50. data/lib/ui_bibz/ui/jumbotron.rb +58 -0
  51. data/lib/ui_bibz/ui/label.rb +58 -0
  52. data/lib/ui_bibz/ui/list/components/list.rb +105 -0
  53. data/lib/ui_bibz/ui/list/list_group.rb +78 -0
  54. data/lib/ui_bibz/ui/nav/components/nav_link.rb +68 -0
  55. data/lib/ui_bibz/ui/nav/nav.rb +65 -0
  56. data/lib/ui_bibz/ui/panel.rb +101 -0
  57. data/lib/ui_bibz/ui/table/components/actions.rb +44 -0
  58. data/lib/ui_bibz/ui/table/components/column.rb +34 -0
  59. data/lib/ui_bibz/ui/table/components/columns.rb +16 -0
  60. data/lib/ui_bibz/ui/table/components/store.rb +67 -0
  61. data/lib/ui_bibz/ui/table/components/table_action.rb +21 -0
  62. data/lib/ui_bibz/ui/table/table.rb +146 -0
  63. data/lib/ui_bibz/ui/table/table_pagination.rb +29 -0
  64. data/lib/ui_bibz/ui/table/table_pagination_per_page.rb +53 -0
  65. data/lib/ui_bibz/ui/table/table_panel.rb +66 -0
  66. data/lib/ui_bibz/ui/table/table_search_field.rb +62 -0
  67. data/lib/ui_bibz/ui/table/ux/actionable.rb +59 -0
  68. data/lib/ui_bibz/ui/table/ux/paginable.rb +35 -0
  69. data/lib/ui_bibz/ui/table/ux/searchable.rb +47 -0
  70. data/lib/ui_bibz/ui/table/ux/sortable.rb +93 -0
  71. data/lib/ui_bibz/ui/ui.rb +14 -0
  72. data/lib/ui_bibz/utils/internationalization.rb +30 -0
  73. data/lib/ui_bibz/version.rb +3 -0
  74. data/lib/ui_bibz.rb +60 -0
  75. data/test/dummy/README.rdoc +28 -0
  76. data/test/dummy/Rakefile +6 -0
  77. data/test/dummy/app/assets/images/.keep +0 -0
  78. data/test/dummy/app/assets/javascripts/application.js +13 -0
  79. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  80. data/test/dummy/app/controllers/application_controller.rb +5 -0
  81. data/test/dummy/app/controllers/concerns/.keep +0 -0
  82. data/test/dummy/app/helpers/application_helper.rb +2 -0
  83. data/test/dummy/app/mailers/.keep +0 -0
  84. data/test/dummy/app/models/.keep +0 -0
  85. data/test/dummy/app/models/concerns/.keep +0 -0
  86. data/test/dummy/app/models/user.rb +5 -0
  87. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  88. data/test/dummy/bin/bundle +3 -0
  89. data/test/dummy/bin/rails +4 -0
  90. data/test/dummy/bin/rake +4 -0
  91. data/test/dummy/bin/setup +29 -0
  92. data/test/dummy/config/application.rb +26 -0
  93. data/test/dummy/config/boot.rb +5 -0
  94. data/test/dummy/config/database.yml +25 -0
  95. data/test/dummy/config/environment.rb +5 -0
  96. data/test/dummy/config/environments/development.rb +41 -0
  97. data/test/dummy/config/environments/production.rb +79 -0
  98. data/test/dummy/config/environments/test.rb +42 -0
  99. data/test/dummy/config/initializers/assets.rb +11 -0
  100. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  101. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  102. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  103. data/test/dummy/config/initializers/inflections.rb +16 -0
  104. data/test/dummy/config/initializers/mime_types.rb +4 -0
  105. data/test/dummy/config/initializers/session_store.rb +3 -0
  106. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  107. data/test/dummy/config/locales/en.yml +23 -0
  108. data/test/dummy/config/routes.rb +57 -0
  109. data/test/dummy/config/secrets.yml +22 -0
  110. data/test/dummy/config.ru +4 -0
  111. data/test/dummy/db/migrate/20150123191721_user.rb +4 -0
  112. data/test/dummy/db/migrate/20150123191805_create_users.rb +12 -0
  113. data/test/dummy/db/schema.rb +25 -0
  114. data/test/dummy/lib/assets/.keep +0 -0
  115. data/test/dummy/log/.keep +0 -0
  116. data/test/dummy/public/404.html +67 -0
  117. data/test/dummy/public/422.html +67 -0
  118. data/test/dummy/public/500.html +66 -0
  119. data/test/dummy/public/favicon.ico +0 -0
  120. data/test/dummy/test/fixtures/users.yml +13 -0
  121. data/test/dummy/test/models/user_test.rb +7 -0
  122. data/test/factories/user.rb +8 -0
  123. data/test/store_test.rb +46 -0
  124. data/test/support/factory_girl.rb +6 -0
  125. data/test/test_helper.rb +23 -0
  126. data/test/ui/button_test.rb +56 -0
  127. data/test/ui/component_test.rb +21 -0
  128. data/test/ui/dropdown_test.rb +18 -0
  129. data/test/ui/glyph_test.rb +23 -0
  130. data/test/ui/grid_test.rb +46 -0
  131. data/test/ui/list_group_test.rb +28 -0
  132. data/test/ui/nav_test.rb +27 -0
  133. data/test/ui/panel_test.rb +52 -0
  134. data/test/ui/table_test.rb +178 -0
  135. data/test/ui_bibz_test.rb +13 -0
  136. data/test/ui_helper_test.rb +44 -0
  137. data/ui_bibz.gemspec +36 -0
  138. data/vendor/assets/javascripts/ui_bibz.js.coffee +24 -0
  139. data/vendor/assets/stylesheets/ui_bibz.sass +46 -0
  140. metadata +398 -0
@@ -0,0 +1,101 @@
1
+ module UiBibz::Ui
2
+
3
+ # Create a panel
4
+ #
5
+ # This element is an extend of UiBibz::Ui::Component.
6
+ # You can use tap method to add header, body or footer element.
7
+ #
8
+ # ==== Attributes
9
+ #
10
+ # * +content+ - Content of element
11
+ # * +options+ - Options of element
12
+ # * +html_options+ - Html Options of element
13
+ #
14
+ # ==== Options
15
+ #
16
+ # You can add HTML attributes using the +html_options+.
17
+ # You can pass arguments in options attribute:
18
+ # * +state+ - State of élement with symbol value:
19
+ # (+:default+, +:primary+, +:info+, +:warning+, +:danger+)
20
+ #
21
+ # ==== Signatures
22
+ #
23
+ # UiBibz::Ui::Panel.new(content, options = nil, html_options = nil)
24
+ #
25
+ # UiBibz::Ui::Panel.new(options = nil, html_options = nil) do
26
+ # content
27
+ # end
28
+ #
29
+ # UiBibz::Ui::Panel.new(options = nil, html_options = nil).tap do |p|
30
+ # p.header content = nil, options = nil, html_options = nil, &block
31
+ # p.body content = nil, options = nil, html_options = nil, &block
32
+ # p.footer content = nil, options = nil, html_options = nil, &block
33
+ # end
34
+ #
35
+ # ==== Examples
36
+ #
37
+ # UiBibz::Ui::Panel('test').render
38
+ #
39
+ # UiBibz::Ui::Panel(state: :primary) do |d|
40
+ # 'test'
41
+ # end.render
42
+ #
43
+ # UiBibz::Ui::Panel.new().tap do |p|
44
+ # p.header 'header', glyph: 'eye', class: 'header-test'
45
+ # p.body do
46
+ # 'body'
47
+ # end
48
+ # p.footer 'footer'
49
+ # end.render
50
+ #
51
+ class Panel < Component
52
+
53
+ def initialize content = nil, options = nil, html_options = nil, &block
54
+ super
55
+ end
56
+
57
+ # Header is a component element
58
+ def header content = nil, options = nil, html_options = nil, &block
59
+ @header = Component.new content, options, html_options, &block
60
+ end
61
+
62
+ # Body is a component element
63
+ def body content = nil, options = nil, html_options = nil, &block
64
+ @body = Component.new content, options, html_options, &block
65
+ end
66
+
67
+ # Footer is a component element
68
+ def footer content = nil, options = nil, html_options = nil, &block
69
+ @footer = Component.new content, options, html_options, &block
70
+ end
71
+
72
+ def render
73
+ content_tag :div, class_and_html_options("panel") do
74
+ concat(header_html) unless @header.nil?
75
+ concat(body_html) if !@body.nil? || !@content.nil?
76
+ concat(footer_html) unless @footer.nil?
77
+ end
78
+ end
79
+
80
+ def state
81
+ sym = @options[:state] || :default
82
+ "panel-#{ states[sym] }"
83
+ end
84
+
85
+ protected
86
+
87
+ def header_html
88
+ content_tag :div, @header.render, @header.class_and_html_options("panel-heading")
89
+ end
90
+
91
+ def body_html
92
+ @body = Component.new @content unless @content.nil?
93
+ content_tag :div, @body.render, @body.class_and_html_options("panel-body")
94
+ end
95
+
96
+ def footer_html
97
+ content_tag :div, @footer.render, @footer.class_and_html_options("panel-footer")
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,44 @@
1
+ require "ui_bibz/ui/table/components/table_action"
2
+ module UiBibz::Ui
3
+ class Actions
4
+
5
+ def initialize store
6
+ @store = store
7
+ @actions = []
8
+ end
9
+
10
+ def action content = nil, options = nil, html_options = nil, &block
11
+ @actions << TableAction.new(content, options, html_options, &block).render
12
+ end
13
+
14
+ def list
15
+ @actions.empty? ? defaults_actions : @actions
16
+ end
17
+
18
+ private
19
+
20
+ def defaults_actions
21
+ [
22
+ TableAction.new(show_name, url: { controller: @store.controller, action: 'show', id: :id }, glyph: 'eye').render,
23
+ TableAction.new(edit_name, url:{ controller: @store.controller, action: 'edit', id: :id }, glyph: 'pencil').render,
24
+ TableAction.new(delete_name, url: { controller: @store.controller, id: :id }, method: :delete, glyph: 'trash', data: { confirm: 'Are you sure?' }).render,
25
+ ]
26
+ end
27
+
28
+ def show_name
29
+ defaults = ['ui_bibz.table.actions.defaults.show', 'Show']
30
+ UiBibz::Utils::Internationalization.new("ui_bibz.table.actions.#{ @store.model.to_s.underscore }.show", default: defaults).translate
31
+ end
32
+
33
+ def edit_name
34
+ defaults = ['ui_bibz.table.actions.defaults.edit', 'Edit']
35
+ UiBibz::Utils::Internationalization.new("ui_bibz.table.actions.#{ @store.model.to_s.underscore }.edit", default: defaults).translate
36
+ end
37
+
38
+ def delete_name
39
+ defaults = ['ui_bibz.table.actions.defaults.delete', 'Delete']
40
+ UiBibz::Utils::Internationalization.new("ui_bibz.table.actions.#{ @store.model.to_s.underscore }.delete", default: defaults).translate
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,34 @@
1
+ module UiBibz::Ui
2
+ class Column
3
+
4
+ attr_accessor :hidden, :link, :name, :data_index, :date_format, :sort, :format, :date_format, :count, :custom_sort, :parent
5
+
6
+ def initialize args
7
+ @args = args
8
+ @link = args[:link] # show or edit
9
+ @name = args[:name]
10
+ @order = args[:order]
11
+ @data_index = args[:data_index]
12
+ @date_format = args[:date_format]
13
+ @sort = args[:sort]
14
+ @custom_sort = args[:custom_sort]
15
+ @parent = args[:parent]
16
+ @count = args[:count]
17
+ @format = args[:format]
18
+ @hidden = args[:hidden]
19
+ end
20
+
21
+ def linkable?
22
+ !@link.nil?
23
+ end
24
+
25
+ def order
26
+ @order || 0
27
+ end
28
+
29
+ def hidden?
30
+ @hidden == true
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ module UiBibz::Ui
2
+ class Columns
3
+
4
+ def initialize columns = nil
5
+ @columns = columns || []
6
+ end
7
+
8
+ def column hash
9
+ @columns << Column.new(hash)
10
+ end
11
+
12
+ def list
13
+ @columns
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,67 @@
1
+ require 'ui_bibz/ui/table/components/columns'
2
+ require 'ui_bibz/ui/table/components/column'
3
+ module UiBibz::Ui
4
+ class Store
5
+
6
+ attr_accessor :records
7
+
8
+ def initialize store
9
+ @records = store.records
10
+ @store = store
11
+ @model = store.model
12
+ end
13
+
14
+ def total_pages
15
+ @records.total_pages
16
+ end
17
+
18
+ def per_page
19
+ @records.per_page
20
+ end
21
+
22
+ def total_entries
23
+ @records.total_entries
24
+ end
25
+
26
+ def sort
27
+ @store.sort
28
+ end
29
+
30
+ def direction
31
+ @store.direction
32
+ end
33
+
34
+ def searchable_attributes
35
+ @store.searchable_attributes
36
+ end
37
+
38
+ def current_page
39
+ @records.current_page
40
+ end
41
+
42
+ def limit_value
43
+ @records.limit_value
44
+ end
45
+
46
+ def columns
47
+ @columns ||= Columns.new model.new.attributes.keys.map{ |record| Column.new({ data_index: record, name: record.humanize }) }
48
+ end
49
+
50
+ def model
51
+ @model
52
+ end
53
+
54
+ def search
55
+ @store.search
56
+ end
57
+
58
+ def controller
59
+ @store.controller
60
+ end
61
+
62
+ def action
63
+ @store.action
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ module UiBibz::Ui
2
+ class TableAction < Component
3
+
4
+ DIVIDER = '---'
5
+
6
+ def initialize content = nil, options = nil, html_options = nil, &block
7
+ super
8
+ end
9
+
10
+ def render
11
+ @content == DIVIDER ? DIVIDER : link_html
12
+ end
13
+
14
+ private
15
+
16
+ def link_html
17
+ link_to glyph_and_content_html, @options[:url], class_and_html_options
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,146 @@
1
+ require "ui_bibz/ui/table/components/store"
2
+ require "ui_bibz/ui/table/components/columns"
3
+ require "ui_bibz/ui/table/components/column"
4
+ require "ui_bibz/ui/table/components/actions"
5
+ require "ui_bibz/ui/table/ux/paginable"
6
+ require "ui_bibz/ui/table/ux/paginable"
7
+ require "ui_bibz/ui/table/ux/searchable"
8
+ require "ui_bibz/ui/table/ux/sortable"
9
+ require "ui_bibz/ui/table/ux/actionable"
10
+ module UiBibz::Ui
11
+
12
+ # Create a Table
13
+ #
14
+ # This element is an extend of UiBibz::Ui::Component.
15
+ #
16
+ # ==== Attributes
17
+ #
18
+ # * +content+ - Content of element
19
+ # * +options+ - Options of element
20
+ # * +html_options+ - Html Options of element
21
+ #
22
+ # ==== Options
23
+ #
24
+ # You can add HTML attributes using the +html_options+.
25
+ # You can pass arguments in options attribute:
26
+ # * +store+ - Store generate by '+table_search_pagination+' method
27
+ # * +url+ - String
28
+ # * tap - Boolean
29
+ # * columns - Add column
30
+ # * actions - Add action by row
31
+ #
32
+ # ==== Signatures
33
+ #
34
+ # UiBibz::Ui::Table.new(store: @store)
35
+ #
36
+ # UiBibz::Ui::Table.new(store: @store, tap: true) do |t|
37
+ # t.columns do |c|
38
+ # c.column name: '#', data_index: '#'
39
+ # end
40
+ # t.action do |a|
41
+ # a.action '', url: url, glyph: ''
42
+ # end
43
+ # end
44
+ #
45
+ # ==== Examples
46
+ #
47
+ # UiBibz::Ui::Table.new(store: @users).render
48
+ #
49
+ # UiBibz::Ui::Table.new(store: @users).tap do |t|
50
+ # t.columns do |c|
51
+ # c.column name: '#', data_index: 'id'
52
+ # c.column name: 'Name fr', data_index: 'name_fr', link: edit_user_path(:id), order: 2
53
+ # c.column name: 'Name en', data_index: 'name_en', order: 1
54
+ # c.column name: 'Name en', data_index: 'name_en', format: lambda{ |records, record| "name #{ record.id}"}
55
+ # end
56
+ # t.action do |a|
57
+ # a.action 'toto', url: users_path(:id), glyph: 'eye'
58
+ # a.action '---'
59
+ # a.action 'momo', url: users_path(:id), glyph: 'home'
60
+ # end
61
+ # end.render
62
+ #
63
+ class Table < Component
64
+
65
+ attr_accessor :columns
66
+
67
+ def initialize content = nil, options = nil, html_options = nil, &block
68
+ super
69
+ @columns = Columns.new
70
+ @actions = Actions.new store
71
+ end
72
+
73
+ def columns &block
74
+ @columns.tap(&block)
75
+ end
76
+
77
+ def actions &block
78
+ @actions.tap(&block)
79
+ end
80
+
81
+ def render
82
+ table_html
83
+ end
84
+
85
+ def store
86
+ @store ||= if @options[:store].nil?
87
+ raise 'Store is nil!'
88
+ elsif @options[:store].try(:records).nil?
89
+ raise 'Store can be created only with "table_search_pagination" method!'
90
+ else
91
+ Store.new @options.delete :store
92
+ end
93
+ end
94
+
95
+ protected
96
+
97
+ def sort
98
+ @sort ||= Sortable.new store, @options
99
+ end
100
+
101
+ def action
102
+ @action ||= Actionable.new store, @options, @actions
103
+ end
104
+
105
+ def cols
106
+ @columns.list.empty? ? store.columns.list : @columns.list
107
+ end
108
+
109
+ def type
110
+ "table-#{ @options[:type] }"
111
+ end
112
+
113
+ def table_html
114
+ content_tag(:table, class_and_html_options(["table", type])) do
115
+
116
+ ths = cols.collect do |col|
117
+ content_tag(:th, sort.header(col)) unless col.hidden?
118
+ end
119
+
120
+ ths = action.header ths
121
+ concat content_tag(:tr, ths.join.html_safe)
122
+
123
+ store.records.each do |record|
124
+ tds = cols.collect do |col|
125
+ content_tag(:td, td_content(record, col)) unless col.hidden?
126
+ end
127
+
128
+ tds = action.body record, tds
129
+ concat content_tag :tr, tds.join.html_safe
130
+ end
131
+ end
132
+ end
133
+
134
+ # Maybe create a class for td_content
135
+ def td_content record, col
136
+ content = col.count ? record.send(col.data_index).count : record.send(col.data_index)
137
+ unless content.nil?
138
+ content = content.strftime(col.date_format) unless col.date_format.nil?
139
+ content = link_to content, action.inject_url(col.link, record) unless col.link.nil?
140
+ content = col.format.call(@store.records, record) unless col.format.nil?
141
+ content
142
+ end
143
+ end
144
+
145
+ end
146
+ end
@@ -0,0 +1,29 @@
1
+ require 'will_paginate'
2
+ require "will_paginate-bootstrap"
3
+ require "ui_bibz/ui/table/components/store"
4
+ module UiBibz::Ui
5
+ class TablePagination < Ui
6
+ include WillPaginate::ActionView
7
+
8
+ def initialize options = nil, html_options = nil
9
+ @pagination = Component.new nil, options, html_options
10
+ end
11
+
12
+ def render
13
+ will_paginate(store.records, params: { controller: store.controller }, renderer: BootstrapPagination::Rails)
14
+ end
15
+
16
+ private
17
+
18
+ def store
19
+ @store ||= if @pagination.options[:store].nil?
20
+ raise 'Store is nil!'
21
+ elsif @pagination.options[:store].try(:records).nil?
22
+ raise 'Store can be created only with "table_search_pagination" method!'
23
+ else
24
+ Store.new @pagination.options.delete :store
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ require "ui_bibz/ui/table/components/store"
2
+ module UiBibz::Ui
3
+ class TablePaginationPerPage < Ui
4
+ include WillPaginate::ActionView
5
+
6
+ def initialize options = nil, html_options = nil
7
+ @per_page_field = Component.new nil, options, html_options
8
+ end
9
+
10
+ def render
11
+ if @per_page_field.options[:wrap_form] != false
12
+ form_tag(url_for(controller: store.controller, action: store.action), method: :get) do
13
+ per_page_html
14
+ end
15
+ else
16
+ per_page_html
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def store
23
+ @store ||= if @per_page_field.options[:store].nil?
24
+ raise 'Store is nil!'
25
+ elsif @per_page_field.options[:store].try(:records).nil?
26
+ raise 'Store can be created only with "table_search_pagination" method!'
27
+ else
28
+ Store.new @per_page_field.options.delete :store
29
+ end
30
+ end
31
+
32
+ def per_page_html
33
+ content_tag :div, class: 'table-pagination-per-page' do
34
+ concat results_count_html
35
+ concat UiBibz::Utils::Internationalization.new("ui_bibz.table.pagination.per_page", default: "Per page: ").translate
36
+ concat select_tag('per_page', options_for_select([25, 50, 100], store.per_page), class: 'form-control')
37
+ end
38
+ end
39
+
40
+ def results_count_html
41
+ "#{ page_entries_info store.records } | ".html_safe
42
+ end
43
+
44
+ def from_current_results
45
+ store.limit_value * store.current_page - store.limit_value + 1
46
+ end
47
+
48
+ def to_current_results
49
+ store.limit_value * store.current_page
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ module UiBibz::Ui
2
+ class TablePanel < Panel
3
+
4
+ attr_accessor :columns
5
+
6
+ def initialize content = nil, options = nil, html_options = nil, &block
7
+ super
8
+ @store = @options.delete(:store) if @options[:store]
9
+ table_options = (@options[:table_options] || {}).merge({ store: @store })
10
+ @table = UiBibz::Ui::Table.new(table_options, @options[:table_html_options])
11
+ end
12
+
13
+ def render
14
+ initialize_header
15
+ initialize_footer
16
+
17
+ content_tag :div, class_and_html_options(panel_classes) do |f|
18
+ form_tag(url_for(controller: @store.controller, action: @store.action), method: :get) do
19
+ concat(header_html) unless @header.nil?
20
+ concat(body_html) unless @body.nil?
21
+ concat(table_html) unless @store.nil?
22
+ concat(footer_html) unless @footer.nil?
23
+ end
24
+ end
25
+ end
26
+
27
+ def columns &block
28
+ @table.columns &block
29
+ end
30
+
31
+ def actions &block
32
+ @table.actions &block
33
+ end
34
+
35
+ private
36
+
37
+ def table_html
38
+ content_tag :div, @table.render, class: 'panel-table'
39
+ end
40
+
41
+ def panel_classes
42
+ %w(panel panel-default table-panel)
43
+ end
44
+
45
+ def search
46
+ @search ||= Searchable.new @store, @options
47
+ end
48
+
49
+ def pagination
50
+ @pagination ||= Paginable.new @store, @options
51
+ end
52
+
53
+ def initialize_footer
54
+ @footer = @table.pagination
55
+ end
56
+
57
+ def initialize_header
58
+ @header = Component.new search.render
59
+ end
60
+
61
+ def initialize_footer
62
+ @footer = Component.new(pagination.render) if pagination.paginable?
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,62 @@
1
+ require "ui_bibz/ui/table/components/store"
2
+ module UiBibz::Ui
3
+ class TableSearchField < Ui
4
+
5
+ def initialize options = nil, html_options = nil
6
+ @search_field = Component.new nil, options, html_options
7
+ end
8
+
9
+ def render
10
+ if @search_field.options[:wrap_form] != false
11
+ form_tag(url_for(controller: store.controller, action: store.action), method: :get) do
12
+ search_field_html
13
+ end
14
+ else
15
+ search_field_html
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def store
22
+ @store ||= if @search_field.options[:store].nil?
23
+ raise 'Store is nil!'
24
+ elsif @search_field.options[:store].try(:records).nil?
25
+ raise 'Store can be created only with "table_search_pagination" method!'
26
+ else
27
+ Store.new @search_field.options.delete :store
28
+ end
29
+ end
30
+
31
+ def search_field_html
32
+ content_tag :div, @search_field.class_and_html_options(div_class) do
33
+ concat content_tag(:span, Glyph.new('search').render, class: 'input-group-addon')
34
+ concat tag(:input, type: 'search', value: store.search, name: 'search', class: 'form-control', placeholder: search_placeholder_field)
35
+ concat content_tag(:span, Glyph.new('times-circle').render, class: 'clear-search-btn input-group-addon')
36
+ end
37
+ end
38
+
39
+ def div_class
40
+ %w(input-group input-group-sm table-search-field)
41
+ end
42
+
43
+ def search_placeholder_field
44
+ UiBibz::Utils::Internationalization.new('ui_bibz.grid.searchable.field.placeholder', searchable_attributes: searchable_attributes_sentence).translate
45
+ end
46
+
47
+ def searchable_attributes_sentence
48
+ store.searchable_attributes.map do |i|
49
+ UiBibz::Utils::Internationalization.new("ui_bibz.grid.searchable.field.searchable_attributes.#{ model_name }.#{ i }", default: [translate_searchable_attributes_by_active_record(i), i.to_s]).translate
50
+ end.to_sentence(locale: I18n.locale)
51
+ end
52
+
53
+ def model_name
54
+ store.model.to_s.underscore
55
+ end
56
+
57
+ def translate_searchable_attributes_by_active_record attr
58
+ store.model.human_attribute_name(attr)
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,59 @@
1
+ module UiBibz::Ui
2
+ class Actionable < Ui
3
+
4
+ def initialize store, options, actions = nil
5
+ @store = store
6
+ @options = options
7
+ @actions = actions
8
+ end
9
+
10
+ def actionable?
11
+ @options[:actionable].nil? ? true : @options[:actionable]
12
+ end
13
+
14
+ def header ths
15
+ ths << content_tag(:th, '', class: 'action') if actionable?
16
+ ths
17
+ end
18
+
19
+ def body record, tds
20
+ tds << td_action(record) if actionable?
21
+ tds
22
+ end
23
+
24
+ # Add "id" in url to match with current record
25
+ def inject_url url, record
26
+ url.gsub(/(\/id\/?)/, "/#{ record.id }/")
27
+ end
28
+
29
+ private
30
+
31
+ def dropdown_action record
32
+ UiBibz::Ui::Dropdown.new(dropdown_action_name, { position: :right, glyph: actions_glyph }, class: 'btn-group-xs').tap do |d|
33
+ unless @actions.nil?
34
+ actions_links(record).each do |l|
35
+ d.list l.html_safe
36
+ end
37
+ end
38
+ end.render
39
+ end
40
+
41
+ def actions_glyph
42
+ { name: 'ellipsis-v', type: 'fw' }
43
+ end
44
+
45
+ def dropdown_action_name
46
+ defaults = ["ui_bibz.table.actions.defaults.title", 'Actions']
47
+ UiBibz::Utils::Internationalization.new("ui_bibz.table.actions.#{ @store.model.to_s.underscore }.title", default: defaults).translate
48
+ end
49
+
50
+ def actions_links record
51
+ @actions.list.compact.map{ |l| inject_url(l, record) }
52
+ end
53
+
54
+ def td_action record
55
+ content_tag :td, dropdown_action(record)
56
+ end
57
+
58
+ end
59
+ end