trestle 0.9.6 → 0.9.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -70,6 +70,7 @@ Trestle.configure do |config|
70
70
  # Specify helper modules to expose to the admin.
71
71
  #
72
72
  # config.helper :all
73
+ # config.helper -> { CustomHelper }
73
74
 
74
75
  # Register callbacks to run before, after or around all Trestle actions.
75
76
  #
@@ -123,7 +124,7 @@ Trestle.configure do |config|
123
124
  # def render; end
124
125
  # end
125
126
  #
126
- # config.form_field :custom, CustomFormField
127
+ # config.form_field :custom, -> { CustomFormField }
127
128
 
128
129
  # == Debugging Options
129
130
  #
@@ -15,6 +15,12 @@ module Trestle
15
15
  options.fetch(name) {
16
16
  if defaults.key?(name)
17
17
  value = defaults[name]
18
+
19
+ # Avoid references to the same instance of a default value
20
+ if value.respond_to?(:dup) && !value.is_a?(Proc)
21
+ value = value.dup
22
+ end
23
+
18
24
  assign(name, value)
19
25
  end
20
26
  }
@@ -54,18 +54,17 @@ module Trestle
54
54
  menus << Navigation::Block.new(&block)
55
55
  end
56
56
 
57
-
58
57
  ## Extension Options
59
58
 
60
59
  # [Internal] List of helper modules to include in all Trestle controllers
61
- option :helpers, []
60
+ option :helpers, Lazy::List.new
62
61
 
63
62
  # [Internal] Container module for block-defined helpers
64
63
  option :helper_module, Module.new
65
64
 
66
65
  # Register global helpers available to all Trestle admins
67
66
  def helper(*helpers, &block)
68
- self.helpers += helpers
67
+ self.helpers << helpers
69
68
  self.helper_module.module_eval(&block) if block_given?
70
69
  end
71
70
 
@@ -82,8 +81,8 @@ module Trestle
82
81
  option :default_adapter, Adapters.compose(Adapters::ActiveRecordAdapter, Adapters::DraperAdapter)
83
82
 
84
83
  # Register a custom form field class
85
- def form_field(name, klass)
86
- Form::Builder.register(name, klass)
84
+ def form_field(name, field)
85
+ Form::Builder.register(name, field)
87
86
  end
88
87
 
89
88
  # [Internal] List of registered hooks
@@ -9,7 +9,7 @@ module Trestle
9
9
  undef_method :display
10
10
 
11
11
  cattr_accessor :fields
12
- self.fields = {}
12
+ self.fields = Lazy::Hash.new
13
13
 
14
14
  def errors(name)
15
15
  if object.respond_to?(:errors) && object.errors.respond_to?(:[])
@@ -19,14 +19,14 @@ module Trestle
19
19
  end
20
20
  end
21
21
 
22
- def self.register(name, klass)
22
+ def self.register(name, field)
23
23
  rename_existing_helper_method(name)
24
- self.fields[name] = klass
24
+ self.fields[name] = field
25
25
  end
26
26
 
27
27
  protected
28
28
  def respond_to_missing?(name, include_all=false)
29
- self.class.fields.has_key?(name) || super
29
+ self.class.fields.key?(name) || super
30
30
  end
31
31
 
32
32
  def method_missing(name, *args, &block)
@@ -0,0 +1,56 @@
1
+ module Trestle
2
+ module Lazy
3
+ module Constantize
4
+ def constantize(value)
5
+ case value
6
+ when String
7
+ value.safe_constantize
8
+ when Proc
9
+ value.call
10
+ else
11
+ value
12
+ end
13
+ end
14
+ end
15
+
16
+ class List
17
+ include Enumerable
18
+ include Constantize
19
+
20
+ def initialize(*items)
21
+ @list = items
22
+ end
23
+
24
+ def each(&block)
25
+ @list.each do |item|
26
+ yield constantize(item)
27
+ end
28
+ end
29
+
30
+ def <<(items)
31
+ @list += Array(items)
32
+ end
33
+ end
34
+
35
+ class Hash
36
+ include Enumerable
37
+ include Constantize
38
+
39
+ delegate :[]=, :key?, to: :@hash
40
+
41
+ def initialize
42
+ @hash = {}
43
+ end
44
+
45
+ def each(&block)
46
+ @hash.each do |key, value|
47
+ yield key, constantize(value)
48
+ end
49
+ end
50
+
51
+ def [](key)
52
+ constantize(@hash[key])
53
+ end
54
+ end
55
+ end
56
+ end
@@ -17,7 +17,7 @@ module Trestle
17
17
  end
18
18
 
19
19
  def <=>(other)
20
- priority <=> other.priority
20
+ [priority, name] <=> [other.priority, other.name]
21
21
  end
22
22
 
23
23
  def priority
@@ -108,7 +108,7 @@ module Trestle
108
108
 
109
109
  def column_value(instance)
110
110
  if @column.block
111
- if defined?(Haml) && Haml::Helpers.block_is_haml?(@column.block)
111
+ if block_is_legacy_haml?
112
112
  # In order for table column blocks to work properly within Haml templates,
113
113
  # the _hamlout local variable needs to be defined in the scope of the block,
114
114
  # so that the Haml version of the capture method is used. Because we
@@ -131,6 +131,10 @@ module Trestle
131
131
  instance.send(@column.field)
132
132
  end
133
133
  end
134
+
135
+ def block_is_legacy_haml?
136
+ defined?(Haml) && Haml::Helpers.respond_to?(:block_is_haml?) && Haml::Helpers.block_is_haml?(@column.block)
137
+ end
134
138
  end
135
139
  end
136
140
  end
@@ -1,3 +1,3 @@
1
1
  module Trestle
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.7"
3
3
  end
data/lib/trestle.rb CHANGED
@@ -11,6 +11,7 @@ module Trestle
11
11
  require_relative "trestle/adapters"
12
12
  require_relative "trestle/attribute"
13
13
  require_relative "trestle/breadcrumb"
14
+ require_relative "trestle/lazy"
14
15
  require_relative "trestle/configurable"
15
16
  require_relative "trestle/configuration"
16
17
  require_relative "trestle/display"
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trestle",
3
- "version": "0.9.6",
3
+ "version": "0.9.7",
4
4
  "description": "A modern, responsive admin framework for Ruby on Rails",
5
5
  "homepage": "https://trestle.io",
6
6
  "repository": "https://github.com/TrestleAdmin/trestle.git",