Module: JavyTool::Breadcrumb::Helpers

Defined in:
lib/javy_tool/breadcrumb.rb

Instance Method Summary collapse

Instance Method Details

#footObject

add javascript to foot



177
178
179
180
181
# File 'lib/javy_tool/breadcrumb.rb', line 177

def foot
  content_for(:foot) do
    yield.html_safe
  end
end

#format_timestamp(ts, format = '%Y-%m-%d %H:%M') ⇒ Object

format time



183
184
185
# File 'lib/javy_tool/breadcrumb.rb', line 183

def format_timestamp(ts,format='%Y-%m-%d %H:%M')
  ts.strftime(format)
end

#head(head_identity = nil) ⇒ Object

set head description,keywords etc



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/javy_tool/breadcrumb.rb', line 164

def head(head_identity=nil)
  content_for(:head) do
    case head_identity
    when "description"
      "<meta name=\"description\" content=\"#{yield}\" />\n"
    when "keywords"
      "<meta name=\"keywords\"  content=\"#{yield}\" />\n"
    else
      yield
    end.html_safe
  end
end

add nested object to the parent form by ajax parameters: name: link title f: form_for’s f association: association object example: <%=link_to_add_fields(“Add Cycle”,f,:pipgame_cycles,f.object.pipgame_cycles.new(:item_type => “App”,:position => “cycle_img”)) %> this method will render _pipgame_cycle_fields.html.erb file like

<div class="fields" rel="need_upload">
  <p>
  <%= f.label :img_src %><br />
  <%= f.hidden_field :img_src,:class=>"web_img_src" %>
  <%= f.hidden_field :item_type %>
  <%= f.hidden_field :position %>
  <%= f.hidden_field :_destroy %>
  <%= link_to_function "remove", "remove_fields(this)" %>
  </p>
</div>

and you need add javascript like following:

function remove_fields(link) {
 $(link).prev("input[type=hidden]").val("1");
 $(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
 var new_id = new Date().getTime();
 var regexp = new RegExp("new_" + association, "g");
 var con = content.replace(regexp, new_id);
 //console.log(con);
 $(link).parent().before(con);
}

deprecated in rails4 need to fix the link_to_founction
Fixed:

Need change the association_fields like following:

<div class="fields" rel="need_upload">
  <p>
  <%= f.label :img_src %><br />
  <%= f.hidden_field :img_src,:class=>"web_img_src" %>
  <%= f.hidden_field :item_type %>
  <%= f.hidden_field :position %>
  <%= f.hidden_field :_destroy %>
  <%= link_to "remove_fields", "#" %>
  </p>
</div>

Need add code to application.js like following:

$(document).bind("click",".add_fields",function(e){
  e.preventDefault();
  var _this = $(this);
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + _this.data("association"), "g");
  var con = _this.data("content").replace(regexp, new_id);
  $(this).parent().before(con);
}).bind("click",".remove_fields",function(e){
  e.preventDefault();
  var _this = $(this);
  _this.prev("input[type=hidden]").val("1");
  _this.closest(".fields").hide();

});


153
154
155
156
157
158
159
160
# File 'lib/javy_tool/breadcrumb.rb', line 153

def link_to_add_fields(name, f, association,new_object=nil,options={})
  new_object ||= f.object.class.reflect_on_association(association).klass.new()
  fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
    render(association.to_s.singularize + "_fields", :f => builder)
  end
  options.merge!(data: {association: association,content: escape_javascript(fields.html_safe)},class: "add_fields")
  link_to(name,"#",options)
end

#notice_message(cls: 'alert-box') ⇒ Object

display the flash messages using foundation



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/javy_tool/breadcrumb.rb', line 56

def notice_message(cls: 'alert-box')
  flash_messages = []
  flash.each do |type, message|
    next if message.nil?
    type = :info if type == :notice
    type = :alert if type == :error
    text = (:div, message.html_safe, class: "#{cls} #{type}")
    flash_messages << text if message
  end
  flash_messages.join("\n").html_safe
end

#render_body_tagObject



48
49
50
51
52
# File 'lib/javy_tool/breadcrumb.rb', line 48

def render_body_tag
  class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
  id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
  raw(%Q[ <body#{id_attribute} class="#{class_attribute}"> ])
end

#render_breadcrumbObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/javy_tool/breadcrumb.rb', line 72

def render_breadcrumb
  return "" if @breadcrumbs.nil? || @breadcrumbs.size <= 0
  prefix = "".html_safe
  crumb = []#.html_safe

  @breadcrumbs.each_with_index do |c, i|
    breadcrumb_class = []
    breadcrumb_class << "current" if i == (@breadcrumbs.length - 1)

    crumb.push (:li, c ,:class => breadcrumb_class )
  end
  return prefix + (:ul, crumb.join("").html_safe, :class => "breadcrumbs")
end

#render_page_titleObject

set SITE_NAME in enviroment.rb set @page_title in controller respectively add<%= render_page_title %> in head



43
44
45
46
# File 'lib/javy_tool/breadcrumb.rb', line 43

def render_page_title
  title = @page_title ? "#{@page_title}_#{SITE_NAME}" : SITE_NAME rescue "SITE_NAME"
  ("title", title, nil, false)
end

#s(html) ⇒ Object



68
69
70
# File 'lib/javy_tool/breadcrumb.rb', line 68

def s(html)
  sanitize( html, :tags => %w(table thead tbody tr td th ol ul li div span font img sup sub br hr a pre p h1 h2 h3 h4 h5 h6), :attributes => %w(id class style src href size color) )
end

#yield_or_default(message, default_message = "") ⇒ Object



37
38
39
# File 'lib/javy_tool/breadcrumb.rb', line 37

def yield_or_default(message, default_message = "")
  message.nil? ? default_message : message
end