Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb

Direct Known Subclasses

UcbRails::User

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dom_idObject



33
34
35
# File 'lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb', line 33

def dom_id
  model_name.plural
end

.haml_attributes(options = {}) ⇒ Object

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">


45
46
47
48
# File 'lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb', line 45

def haml_attributes(options = {})
  options_out = (options || {}).merge('always_css_class' => 'table')
  haml_attributes_helper(options_out)
end

.haml_attributes_helper(options = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb', line 50

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

Instance Method Details

#dom_id(prefix = nil) ⇒ Object

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"


10
11
12
13
# File 'lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb', line 10

def dom_id(prefix = nil) 
  prefix ||= 'new' unless id
  [ prefix, self.class.model_name.singular, id ].compact * '_'
end

#haml_attributes(options = {}) ⇒ Object

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">


23
24
25
26
27
28
29
# File 'lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb', line 23

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