admin_it 1.0.11 → 1.1.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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile +8 -1
  4. data/README.md +4 -2
  5. data/admin_it.gemspec +1 -0
  6. data/lib/admin_it.rb +7 -5
  7. data/lib/admin_it/actions/action.rb +5 -2
  8. data/lib/admin_it/config.rb +1 -0
  9. data/lib/admin_it/context/collection_context.rb +10 -4
  10. data/lib/admin_it/context/context.rb +12 -13
  11. data/lib/admin_it/context/show_context.rb +6 -5
  12. data/lib/admin_it/context/single_context.rb +19 -8
  13. data/lib/admin_it/context/table_context.rb +9 -7
  14. data/lib/admin_it/context/tiles_context.rb +1 -0
  15. data/lib/admin_it/controller.rb +3 -1
  16. data/lib/admin_it/data.rb +4 -2
  17. data/lib/admin_it/data/active_record.rb +10 -301
  18. data/lib/admin_it/data/active_record/collection_context.rb +47 -0
  19. data/lib/admin_it/data/active_record/field.rb +64 -0
  20. data/lib/admin_it/data/active_record/filter.rb +36 -0
  21. data/lib/admin_it/data/active_record/resource.rb +66 -0
  22. data/lib/admin_it/data/active_record/single_context.rb +102 -0
  23. data/lib/admin_it/data/data_behavior.rb +2 -0
  24. data/lib/admin_it/data/hash.rb +3 -0
  25. data/lib/admin_it/data/mongoid.rb +14 -0
  26. data/lib/admin_it/data/mongoid/field.rb +58 -0
  27. data/lib/admin_it/data/mongoid/resource.rb +67 -0
  28. data/lib/admin_it/data/object.rb +3 -0
  29. data/lib/admin_it/engine.rb +4 -1
  30. data/lib/admin_it/env.rb +21 -2
  31. data/lib/admin_it/field/field.rb +15 -26
  32. data/lib/admin_it/filters/field_filter.rb +1 -0
  33. data/lib/admin_it/filters/filter.rb +8 -13
  34. data/lib/admin_it/filters/value_filter.rb +1 -0
  35. data/lib/admin_it/helpers.rb +2 -0
  36. data/lib/admin_it/helpers/field.rb +3 -3
  37. data/lib/admin_it/helpers/input.rb +1 -0
  38. data/lib/admin_it/helpers/page.rb +2 -0
  39. data/lib/admin_it/helpers/table.rb +12 -8
  40. data/lib/admin_it/helpers/tiles.rb +2 -0
  41. data/lib/admin_it/helpers/toolbar.rb +4 -0
  42. data/lib/admin_it/helpers/top_menu.rb +3 -0
  43. data/lib/admin_it/resource.rb +16 -21
  44. data/lib/admin_it/version.rb +1 -1
  45. data/lib/extend_it.rb +8 -4
  46. data/lib/extend_it/array_of.rb +12 -10
  47. data/lib/extend_it/base.rb +9 -8
  48. data/lib/extend_it/callbacks.rb +11 -12
  49. data/lib/extend_it/dsl.rb +33 -44
  50. data/spec/extend_it/dsl_spec.rb +21 -32
  51. data/spec/lib/data/mongoid/field_spec.rb +28 -0
  52. data/spec/lib/data/mongoid/resource_spec.rb +50 -0
  53. data/spec/spec_helper.rb +8 -3
  54. data/spec/support/module_helpers.rb +20 -0
  55. metadata +30 -13
  56. data/.ruby-version +0 -1
  57. data/lib/admin_it/definitions.rb +0 -89
  58. data/lib/admin_it/utils.rb +0 -21
  59. data/lib/extend_it/asserts.rb +0 -40
  60. data/lib/extend_it/caller.rb +0 -35
  61. data/lib/extend_it/config.rb +0 -30
  62. data/lib/extend_it/ensures.rb +0 -193
  63. data/spec/extend_it/config_spec.rb +0 -52
  64. data/spec/extend_it/ensures_spec.rb +0 -39
@@ -1,10 +1,12 @@
1
1
  module AdminIt
2
2
  module ObjectData
3
+ #
3
4
  module Context
4
5
  def self.included(base)
5
6
  base.extend(ClassMethods)
6
7
  end
7
8
 
9
+ #
8
10
  module ClassMethods
9
11
  def load_fields
10
12
  all = entity_class.instance_methods(false) - Object.instance_methods
@@ -36,6 +38,7 @@ module AdminIt
36
38
  end
37
39
  end
38
40
 
41
+ #
39
42
  module Field
40
43
  protected
41
44
 
@@ -1,11 +1,14 @@
1
+ #
1
2
  module AdminIt
3
+ #
2
4
  class Engine < Rails::Engine
3
5
  # paths['app/controllers'] = File.join('lib', 'admin_it', 'controllers')
4
6
 
5
7
  config.to_prepare do
6
8
  Rails.application.config.i18n.load_path +=
7
9
  Dir[Engine.root.join('lib', 'admin_it', 'locales', '*.yml')]
8
- unless File.basename($0) == "rake" && ARGV.include?("db:migrate")
10
+ unless File.basename($PROGRAM_NAME) == 'rake' &&
11
+ ARGV.include?('db:migrate')
9
12
  Dir[File.join(AdminIt.config.root, '**', '*.rb')].each do |file|
10
13
  require file
11
14
  end
data/lib/admin_it/env.rb CHANGED
@@ -18,10 +18,29 @@ module AdminIt
18
18
 
19
19
  # @private
20
20
  def self.pundit?
21
+ return @pundit unless @pundit.nil?
21
22
  Gem::Specification.find_by_name('pundit')
22
- true
23
+ @pundit = true
23
24
  rescue Gem::LoadError
24
- false
25
+ @pundit = false
26
+ end
27
+
28
+ # @private
29
+ def self.active_record?
30
+ return @active_record unless @active_record.nil?
31
+ Gem::Specification.find_by_name('activerecord')
32
+ @active_record = true
33
+ rescue Gem::LoadError
34
+ @active_record = false
35
+ end
36
+
37
+ # @private
38
+ def self.mongoid?
39
+ return @mongoid unless @mongoid.nil?
40
+ Gem::Specification.find_by_name('mongoid')
41
+ @mongoid = true
42
+ rescue Gem::LoadError
43
+ @mongoid = false
25
44
  end
26
45
 
27
46
  # @private
@@ -2,7 +2,10 @@ require File.join %w(extend_it base)
2
2
  require File.join %w(extend_it dsl)
3
3
  require File.join %w(extend_it callbacks)
4
4
 
5
+ #
5
6
  module AdminIt
7
+ using EnsureIt if EnsureIt.refined?
8
+
6
9
  #
7
10
  # Describes any field of data
8
11
  #
@@ -16,7 +19,7 @@ module AdminIt
16
19
  include ExtendIt::Callbacks
17
20
 
18
21
  TYPES = %i(unknown integer float string date datetime time relation enum
19
- binary)
22
+ array hash range regexp symbol binary)
20
23
 
21
24
  define_callbacks :initialize
22
25
 
@@ -67,15 +70,7 @@ module AdminIt
67
70
  inherited_class_reader :field_name, :entity_class
68
71
 
69
72
  def self.create(name, _entity_class, **opts)
70
- # type: :unknown,
71
- # readable: true,
72
- # writable: true,
73
- # visible: true,
74
- # sortable: true
75
- # )
76
73
  base = self
77
- # _type, _readable, _writable, _visible, _sortable =
78
- # type, readable, writable, visible, sortable
79
74
  Class.new(base) do
80
75
  @field_name, @entity_class = name, _entity_class
81
76
  import_data_module(base)
@@ -88,7 +83,7 @@ module AdminIt
88
83
  end
89
84
 
90
85
  def self.type=(value)
91
- @type = TYPES.include?(value) ? value : TYPES[0]
86
+ @type = value.ensure_symbol(values: TYPES, default: TYPES[0])
92
87
  end
93
88
 
94
89
  def self.placeholder
@@ -185,8 +180,8 @@ module AdminIt
185
180
  protected
186
181
 
187
182
  def read_value(entity)
188
- raise NotImplementedError,
189
- "Attempt to read field #{name} with unimplemented reader"
183
+ fail NotImplementedError,
184
+ "Attempt to read field #{name} with unimplemented reader"
190
185
  end
191
186
 
192
187
  def show_value(entity)
@@ -194,38 +189,32 @@ module AdminIt
194
189
  end
195
190
 
196
191
  def write_value(entity, value)
197
- raise NotImplementedError,
198
- "Attempt to write to field #{name} with unimplemented writer"
192
+ fail NotImplementedError,
193
+ "Attempt to write to field #{name} with unimplemented writer"
199
194
  end
200
195
  end
201
196
 
197
+ #
202
198
  module FieldsHolder
203
199
  extend ExtendIt::DslModule
204
200
 
205
201
  dsl do
206
202
  dsl_hash_of_objects :fields, single: :field do |name, **opts|
207
203
  field_class = opts[:class] || opts[:field_class] || Field
208
- unless field_class.is_a?(Class) && field_class <= Field
209
- fail(
210
- ArgumentError,
211
- 'field class should be AdminIt::Field descendant'
212
- )
213
- end
204
+ field_class.ensure_class(Field)
214
205
  field_class.create(name, entity_class)
215
206
  end
216
207
 
217
208
  def hide_fields(*names)
218
209
  hash = dsl_get(:fields, {})
219
- names.ensure_symbols.each do |name|
220
- hash[name].hide if hash.key?(name)
221
- end
210
+ names = names.ensure_array(:flatten, :ensure_symbol, :compact, :uniq)
211
+ names.each { |name| hash[name].hide if hash.key?(name) }
222
212
  end
223
213
 
224
214
  def show_fields(*names)
225
215
  hash = dsl_get(:fields, {})
226
- names.ensure_symbols.each do |name|
227
- hash[name].show if hash.key?(name)
228
- end
216
+ names = names.ensure_array(:flatten, :ensure_symbol, :compact, :uniq)
217
+ names.each { |name| hash[name].show if hash.key?(name) }
229
218
  end
230
219
  end
231
220
 
@@ -1,4 +1,5 @@
1
1
  module AdminIt
2
+ #
2
3
  class FieldFilter < Filter
3
4
  dsl do
4
5
  dsl_accessor :field
@@ -4,7 +4,10 @@ require File.join %w(extend_it base)
4
4
  require File.join %w(extend_it dsl)
5
5
  require File.join %w(extend_it callbacks)
6
6
 
7
+ #
7
8
  module AdminIt
9
+ using EnsureIt if EnsureIt.refined?
10
+ #
8
11
  class Filter
9
12
  extend ExtendIt::Base
10
13
  extend ExtendIt::Dsl
@@ -43,11 +46,7 @@ module AdminIt
43
46
  protected
44
47
 
45
48
  def create_class(name, _resource)
46
- name = name.ensure_symbol || fail(
47
- ArgumentError,
48
- '`name` argument for `Filter::create_class` should be a Symbol' \
49
- ' or a String'
50
- )
49
+ name = name.ensure_symbol!
51
50
  base = self
52
51
  Class.new(base) do
53
52
  @filter_name, @resource = name, _resource
@@ -142,8 +141,8 @@ module AdminIt
142
141
  (?:\d*\.\d+(?:[eE][+\-]?\d+)?)|
143
142
  (?:\d+(?:\.\d*)?[eE][+\-]?\d+)
144
143
  \z/x
145
- DATE = %q{[0-3]?[0-9][\/.\-][0-3]?[0-9][\/.\-](?:[0-9]{2})?[0-9]{2}}
146
- TIME = %q{[0-2]?[0-9][.:\-][0-5]?[0-9]}
144
+ DATE = %q([0-3]?[0-9][\/.\-][0-3]?[0-9][\/.\-](?:[0-9]{2})?[0-9]{2})
145
+ TIME = %q([0-2]?[0-9][.:\-][0-5]?[0-9])
147
146
  DATE_REGEXP = /\A#{DATE}\z/
148
147
  TIME_REGEXP = /\A#{TIME}\z/
149
148
  DATETIME_REGEXP = /\A#{DATE}(?:\s+|[\/.\-])#{TIME}\z/
@@ -207,18 +206,14 @@ module AdminIt
207
206
  end
208
207
  end
209
208
 
209
+ #
210
210
  module FiltersHolder
211
211
  extend ExtendIt::DslModule
212
212
 
213
213
  dsl do
214
214
  dsl_hash_of_objects :filters, single: :filter do |name, **opts|
215
215
  filter_class = opts[:class] || opts[:filter_class] || Filter
216
- unless filter_class.is_a?(Class) && filter_class <= Filter
217
- fail(
218
- ArgumentError,
219
- 'filter class should be AdminIt::Filter descendant'
220
- )
221
- end
216
+ filter_class.ensure_class!(Filter)
222
217
  filter_class.create(name, entity_class)
223
218
  end
224
219
  end
@@ -1,4 +1,5 @@
1
1
  module AdminIt
2
+ #
2
3
  class ValueFilter < FieldFilter
3
4
  attr_reader :values
4
5
 
@@ -1,6 +1,8 @@
1
1
  require 'wrap_it'
2
2
 
3
+ #
3
4
  module AdminIt
5
+ #
4
6
  module Helpers; end
5
7
  WrapIt.register_module Helpers
6
8
  end
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class Field < WrapIt::Base
4
6
  include WrapIt::TextContainer
5
7
  attr_accessor :field, :context
@@ -9,9 +11,7 @@ module AdminIt
9
11
  argument :context, if: AdminIt::Context
10
12
 
11
13
  before_capture do
12
- unless context.is_a?(AdminIt::Context)
13
- self.context = @template.context
14
- end
14
+ self.context = @template.context unless context.is_a?(AdminIt::Context)
15
15
  if field.is_a?(AdminIt::Field)
16
16
  entity = context.entity
17
17
  body << html_safe(field.show(entity).to_s) if body.empty?
@@ -1,5 +1,6 @@
1
1
  module AdminIt
2
2
  module Helpers
3
+ #
3
4
  class Input < WrapIt::Base
4
5
  html_class 'form-control'
5
6
  attr_accessor :field, :entity
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class Page < WrapIt::Container
4
6
  default_tag 'body'
5
7
 
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class Cell < WrapIt::Base
4
6
  include WrapIt::TextContainer
5
7
  default_tag 'td'
@@ -16,6 +18,7 @@ module AdminIt
16
18
  end
17
19
  end
18
20
 
21
+ #
19
22
  class ActionsCell < WrapIt::Base
20
23
  default_tag 'td'
21
24
 
@@ -27,9 +30,9 @@ module AdminIt
27
30
  buttons = single.map do |_context|
28
31
  if _context <= ShowContext && context.show_in_dialog?
29
32
  '<a class="btn btn-xs btn-info" ' +
30
- %Q{data-toggle="modal" data-target="#confirm_modal" } +
31
- %Q{href="#{_context.path(entity)}?layout=dialog">} +
32
- %Q{<i class="fa fa-#{_context.icon}"></i></a>}
33
+ %Q(data-toggle="modal" data-target="#confirm_modal" ) +
34
+ %Q(href="#{_context.path(entity)}?layout=dialog">) +
35
+ %Q(<i class="fa fa-#{_context.icon}"></i></a>)
33
36
  else
34
37
  cl = _context <= ShowContext ? 'info' : 'default'
35
38
  href = _context.path(entity)
@@ -44,8 +47,8 @@ module AdminIt
44
47
  unless confirm.nil?
45
48
  buttons <<
46
49
  '<a class="btn btn-xs btn-danger" ' +
47
- %Q{data-toggle="modal" data-target="#confirm_modal" } +
48
- %Q{href="#{confirm.path(entity)}} +
50
+ %Q(data-toggle="modal" data-target="#confirm_modal" ) +
51
+ %Q(href="#{confirm.path(entity)}) +
49
52
  '?layout=dialog&confirm=destroy">' +
50
53
  '<i class="fa fa-trash-o"></i></a>'
51
54
  end
@@ -69,11 +72,13 @@ module AdminIt
69
72
  end
70
73
  end
71
74
 
75
+ #
72
76
  class Header < WrapIt::Container
73
77
  default_tag 'tr'
74
78
  child :cell, Cell, tag: 'th'
75
79
  end
76
80
 
81
+ #
77
82
  class Row < WrapIt::Container
78
83
  default_tag 'tr'
79
84
  child :cell, Cell
@@ -81,12 +86,11 @@ module AdminIt
81
86
 
82
87
  before_capture do
83
88
  block = parent.context.row
84
- unless block.nil?
85
- instance_exec(parent.context.entity, &block)
86
- end
89
+ instance_exec(parent.context.entity, &block) unless block.nil?
87
90
  end
88
91
  end
89
92
 
93
+ #
90
94
  class Table < WrapIt::Container
91
95
  default_tag 'table'
92
96
  html_class %w(table)
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class TileHeader < WrapIt::Base
4
6
  include WrapIt::TextContainer
5
7
  default_tag 'h4'
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class ToolbarItem < WrapIt::Link
4
6
  attr_accessor :icon, :add_class, :add_data
5
7
  option :icon
@@ -20,11 +22,13 @@ module AdminIt
20
22
  end
21
23
  end
22
24
 
25
+ #
23
26
  class ToolbarButtons < WrapIt::Container
24
27
  html_class 'btn-group'
25
28
  child :button, ToolbarItem, class: 'btn navbar-btn'
26
29
  end
27
30
 
31
+ #
28
32
  class Toolbar < WrapIt::Container
29
33
  default_tag 'nav'
30
34
  html_class 'navbar'
@@ -1,5 +1,7 @@
1
1
  module AdminIt
2
+ #
2
3
  module Helpers
4
+ #
3
5
  class TopMenuItem < WrapIt::Link
4
6
  attr_accessor :resource
5
7
  option :resource
@@ -19,6 +21,7 @@ module AdminIt
19
21
  end
20
22
  end
21
23
 
24
+ #
22
25
  class TopMenu < WrapIt::Container
23
26
  default_tag 'ul'
24
27
  html_class 'nav'
@@ -1,9 +1,11 @@
1
1
  require File.join %w(extend_it base)
2
2
  require File.join %w(extend_it callbacks)
3
3
 
4
- using ExtendIt::Ensures
5
-
4
+ #
6
5
  module AdminIt
6
+ using EnsureIt if EnsureIt.refined?
7
+
8
+ #
7
9
  class Resource
8
10
  extend ExtendIt::Base
9
11
  include ExtendIt::Callbacks
@@ -47,12 +49,11 @@ module AdminIt
47
49
  end
48
50
 
49
51
  defaults do
50
- fields { puts "FIELDS"; [] }
52
+ fields { [] }
51
53
 
52
- filters { puts "FILTERS"; [] }
54
+ filters { [] }
53
55
 
54
56
  display_name do
55
- puts "DISPLAY"
56
57
  plural.split('_').map { |s| s.capitalize }.join(' ')
57
58
  end
58
59
 
@@ -75,18 +76,13 @@ module AdminIt
75
76
  destroyable: true,
76
77
  auto_filters: true
77
78
  )
78
- name = name.ensure_symbol || fail(
79
- ArgumentError,
80
- '`name` argument for resource constructor should be a Symbol ' \
81
- 'or a String'
82
- )
83
-
79
+ name = name.ensure_symbol!
84
80
  @name, @entity_class = name, entity_class
85
81
  if @entity_class.nil?
86
82
  begin
87
83
  @entity_class = Object.const_get(name.to_s.camelize) # !PORTABLE
88
84
  rescue NameError
89
- fail ArgumentError, "Can't find entity class for #{name}"
85
+ raise ArgumentError, "Can't find entity class for #{name}"
90
86
  end
91
87
  end
92
88
 
@@ -215,15 +211,13 @@ module AdminIt
215
211
  end
216
212
 
217
213
  def i18n_display_name
218
- begin
219
- I18n
220
- .t!("admin_it.resources.#{name}.display_name.plural")
221
- .split(' ')
222
- .map { |s| s.mb_chars.capitalize }
223
- .join(' ')
224
- rescue I18n::MissingTranslationData
225
- nil
226
- end
214
+ I18n
215
+ .t!("admin_it.resources.#{name}.display_name.plural")
216
+ .split(' ')
217
+ .map { |s| s.mb_chars.capitalize }
218
+ .join(' ')
219
+ rescue I18n::MissingTranslationData
220
+ nil
227
221
  end
228
222
 
229
223
  private
@@ -234,6 +228,7 @@ module AdminIt
234
228
  resource_module = data_module.const_get(:Resource)
235
229
  extend(resource_module) if resource_module.is_a?(Module)
236
230
  rescue NameError
231
+ nil
237
232
  end
238
233
  end
239
234