Sha256: 1b9e1af130b3ebe15f15809e3c1915d02a903a8adc31fb30454a184b8eb21a57

Contents?: true

Size: 1.63 KB

Versions: 188

Compression:

Stored size: 1.63 KB

Contents

module RocketCMS
  class PatchDSL
    def self.call(&blk)
      instance = new
      instance.instance_eval(&blk)
      instance.instance_variables.inject({}) { |result_hash, instance_variable|
        result_hash[instance_variable[1..-1].to_sym] = instance.instance_variable_get(instance_variable)
        result_hash
      }
    end
    
    def initialize
      @model  = Proc.new {}
      @admin  = Proc.new {}
      @list   = Proc.new {}
      @show   = Proc.new {}
      @edit   = Proc.new {}
      @export = Proc.new {}
    end
    
    def model(&blk);  @model = blk;  end
    def admin(&blk);  @admin = blk;  end
    def list(&blk);   @list = blk;   end
    def show(&blk);   @show = blk;   end
    def edit(&blk);   @edit = blk;   end
    def export(&blk); @export = blk; end
  end
  
  @@patches = {}
  
  def self.only_patches(s, sections)
    s.instance_eval do
      sections.each do |section|
        send section do
          RocketCMS.apply_patches self
        end
      end
    end
  end
  
  def self.apply_patches(s)
    if s.class.name.index('RailsAdmin::Config::Sections::').nil?
      if s.class.name == 'RailsAdmin::Config::Model'
        s.instance_eval(&@@patches[s.abstract_model.model_name][:admin]) unless @@patches[s.abstract_model.model_name].nil?
      else
        s.instance_eval(&@@patches[s.name][:model]) unless @@patches[s.name].nil?
      end
    else
      model = s.abstract_model.model_name
      action = s.class.name.split('::')[-1].downcase.to_sym
      s.instance_eval(&@@patches[model][action]) unless @@patches[model].nil?
    end
  end
  
  def self.patch(model, &blk)
     @@patches[model] = PatchDSL.call(&blk)
  end
end

Version data entries

188 entries across 188 versions & 2 rubygems

Version Path
rocket_cms-0.37.1 lib/rocket_cms/patch.rb
rocket_cms-0.37.0 lib/rocket_cms/patch.rb
rocket_cms-0.36.0 lib/rocket_cms/patch.rb
rocket_cms-0.35.0 lib/rocket_cms/patch.rb
rocket_cms-0.34.0 lib/rocket_cms/patch.rb
rocket_cms-0.33.1 lib/rocket_cms/patch.rb
rocket_cms-0.33.0 lib/rocket_cms/patch.rb
rocket_cms-0.32.0 lib/rocket_cms/patch.rb
rocket_cms-0.31.1 lib/rocket_cms/patch.rb
rocket_cms-0.31.0 lib/rocket_cms/patch.rb
rocket_cms-0.30.0 lib/rocket_cms/patch.rb
rocket_cms-0.29.0 lib/rocket_cms/patch.rb
rocket_cms-0.28.0 lib/rocket_cms/patch.rb
rocket_cms-0.27.8 lib/rocket_cms/patch.rb
rocket_cms-0.27.7 lib/rocket_cms/patch.rb
rocket_cms-0.27.6 lib/rocket_cms/patch.rb
rocket_cms-0.27.5 lib/rocket_cms/patch.rb
rocket_cms-0.27.4 lib/rocket_cms/patch.rb
rocket_cms-0.27.3 lib/rocket_cms/patch.rb
rocket_cms-0.27.2 lib/rocket_cms/patch.rb