Skip to content
This repository
Browse code

LazyModel: refactor to remove unnecessary #store method

  • Loading branch information...
commit a72f9109b797cb8d0995efcdb81a5400b8c3069d 1 parent fb22472
Taavo Smith authored December 07, 2012
9  lib/rails_admin/config.rb
@@ -244,9 +244,12 @@ def model(entity, &block)
244 244
             entity.class.name.to_sym
245 245
           end
246 246
         end
247  
-        config = @registry[key] ||= RailsAdmin::Config::LazyModel.new(entity)
248  
-        config.store(block) if block
249  
-        config
  247
+
  248
+        if block
  249
+          @registry[key] = RailsAdmin::Config::LazyModel.new(entity, &block)
  250
+        else
  251
+          @registry[key] ||= RailsAdmin::Config::LazyModel.new(entity)
  252
+        end
250 253
       end
251 254
 
252 255
       def default_hidden_fields=(fields)
21  lib/rails_admin/config/lazy_model.rb
@@ -3,27 +3,18 @@
3 3
 module RailsAdmin
4 4
   module Config
5 5
     class LazyModel
6  
-      def initialize(entity)
  6
+      def initialize(entity, &block)
7 7
         @entity = entity
  8
+        @deferred_block = block
8 9
       end
9 10
 
10 11
       def method_missing(method, *args, &block)
11  
-        if @block && not(@block_already_evaluated)
12  
-          @model ||= RailsAdmin::Config::Model.new(@entity)
13  
-          @model.instance_eval(&@block)
14  
-          @block_already_evaluated = true
15  
-        else
16  
-          @model ||= RailsAdmin::Config::Model.new(@entity)
17  
-        end
18  
-        @model.send(method, *args, &block)
19  
-      end
20  
-
21  
-      def store(block)
22  
-        if @block # reset model to not eval twice
  12
+        if !@model
23 13
           @model = RailsAdmin::Config::Model.new(@entity)
24  
-          @block_already_evaluated = false
  14
+          @model.instance_eval(&@deferred_block) if @deferred_block
25 15
         end
26  
-        @block = block
  16
+
  17
+        @model.send(method, *args, &block)
27 18
       end
28 19
     end
29 20
   end
30  spec/rails_admin/config/lazy_model_spec.rb
... ...
@@ -1,41 +1,27 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe RailsAdmin::Config::LazyModel do
4  
-  let(:lazy_model) { RailsAdmin::Config::LazyModel.new(:Team) }
5  
-
6 4
   describe "#store" do
7 5
     let(:block) { Proc.new { register_instance_option('parameter') } } # an arbitrary instance method we can spy on
8 6
     let(:other_block) { Proc.new { register_instance_option('other parameter') } }
9 7
 
10  
-    it "doesn't execute the block immediately" do
  8
+    it "doesn't evaluate the block immediately" do
11 9
       RailsAdmin::Config::Model.any_instance.should_not_receive(:register_instance_option)
12  
-      lazy_model.store(block)
13  
-    end
14 10
 
15  
-    it "executes only when reading" do
16  
-      RailsAdmin::Config::Model.any_instance.should_receive(:register_instance_option).with('parameter')
17  
-      lazy_model.store(block)
18  
-      lazy_model.groups # an arbitrary instance method on RailsAdmin::Config::Model to wake up lazy_model
  11
+      RailsAdmin::Config::LazyModel.new(:Team, &block)
19 12
     end
20 13
 
21  
-    it "evaluates only last block" do
22  
-      RailsAdmin::Config::Model.any_instance.should_not_receive(:register_instance_option).with('parameter')
23  
-      RailsAdmin::Config::Model.any_instance.should_receive(:register_instance_option).with('other parameter')
24  
-      lazy_model.store(block)
25  
-      lazy_model.store(other_block)
26  
-      lazy_model.groups
27  
-    end
  14
+    it "evaluates block when reading" do
  15
+      RailsAdmin::Config::Model.any_instance.should_receive(:register_instance_option).with('parameter')
28 16
 
29  
-    it "resets models when a new block is given" do
30  
-      lazy_model.store(block)
31  
-      lazy_model.groups
32  
-      RailsAdmin::Config::Model.should_receive(:new)
33  
-      lazy_model.store(other_block)
  17
+      lazy_model = RailsAdmin::Config::LazyModel.new(:Team, &block)
  18
+      lazy_model.groups # an arbitrary instance method on RailsAdmin::Config::Model to wake up lazy_model
34 19
     end
35 20
 
36 21
     it "evaluates config block only once" do
37 22
       RailsAdmin::Config::Model.any_instance.should_receive(:register_instance_option).once.with('parameter')
38  
-      lazy_model.store(block)
  23
+
  24
+      lazy_model = RailsAdmin::Config::LazyModel.new(:Team, &block)
39 25
       lazy_model.groups
40 26
       lazy_model.groups
41 27
     end

0 notes on commit a72f910

Please sign in to comment.
Something went wrong with that request. Please try again.