Sha256: 12622b2854a49e58040e8272f1eae245a6189d9407a3d6c6efbb6c0ca54bdcc9

Contents?: true

Size: 1.75 KB

Versions: 13

Compression:

Stored size: 1.75 KB

Contents

class ActiveRecord::Base
  
  # Create dom id for new or persisted instances.  Optionsal _prefix_ parameter.
  #
  #   User.new.dom_id                => "new_user"
  #   User.new.dom_id('my_prefix')   => "my_prefix_user"
  #   User.find(10).dom_id           => "user_10"
  #   User.find(10).dom_id('edit')   => "edit_user_10"
  #
  def dom_id(prefix = nil) 
    prefix ||= 'new' unless id
    [ prefix, self.class.model_name.singular, id ].compact * '_'
  end

  # Assists in using haml object shortcut [] to put attributes on elements
  #
  #   %tr[share]
  #   #=> <tr id="share_1" class="shares">
  #
  #   %tr[share, :class => 'foo', 'data-bar' => 42]  
  #   #=> <tr id="share_1" class="shares foo" data-bar="42">
  #
  def haml_attributes(options = {})
    options_out = (options || {}).merge(
      'id' => dom_id,
      'always_css_class' => self.class.model_name.singular)
      
    self.class.haml_attributes_helper(options_out)
  end

  class << self

    def dom_id
      model_name.plural
    end
    
    # Assists in using haml object shortcut [] to put attributes on elements
    #
    #   %table[Share]
    #   #=> <table id="shares" class="table">
    #
    #   %table[Share, :class => 'foo', 'data-bar' => 42]  
    #   #=> <table id="shares" class="table foo" data-bar="42">
    #
    def haml_attributes(options = {})
      options_out = (options || {}).merge('always_css_class' => 'table')
      haml_attributes_helper(options_out)
    end
    
    def haml_attributes_helper(options = {})
      attributes = options.stringify_keys

      attributes["id"] = attributes.delete("id") || dom_id
      attributes["class"] = [attributes.delete('always_css_class'), attributes.delete("class")].
          flatten.map(&:presence).compact
      attributes
    end

  end
  
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ucb_rails-0.0.14 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.13 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.12 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.11 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.10 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.9 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.8 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.7 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.6 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.5 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.4 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.3 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb
ucb_rails-0.0.2 lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb