Module: BootstrapAdminHelper
- Defined in:
- app/helpers/bootstrap_admin_helper.rb
Instance Method Summary collapse
-
#attributes ⇒ BootstrapAdmin::Attribute Array
Finds the model instance attributes to use for the current action.
-
#available_actions ⇒ Object
.
- #bootstrap_form_url(item) ⇒ Object
- #bootstrap_url_for(options = {}) ⇒ Object (also: #bootstrap_url)
-
#collection_for(controller) ⇒ ActiveRecord::Base Array
.
-
#collection_name_for(controller) ⇒ String
.
-
#display_field(item, field, &block) ⇒ Markup
Builds the markup to properly display a field, including label and field value.
-
#index_actions_for(item) ⇒ Object
.
-
#index_default_actions ⇒ Object
.
-
#link_to(*args, &block) ⇒ String
This re-implementation of link_to simply looks at the link label and if it falls under certain conditions, then it tries to create a label and/or url accordingly.
-
#model_for(controller) ⇒ Class
.
-
#model_instance_for(controller) ⇒ ActiveRecord::Base
.
-
#model_name_for(controller) ⇒ String
.
-
#render_field(item, field) ⇒ Object
.
-
#render_form_field(form, field) ⇒ Object
.
-
#render_with_fallback(*args) ⇒ Object
Tries to render the action/partial “namespaced” with the current action name if it fails, then simply calls render with whatever args that got passed in.
- #show_default_actions ⇒ Object
Instance Method Details
#attributes ⇒ BootstrapAdmin::Attribute Array
Finds the model instance attributes to use for the current action.
First it checks if the bootstrap_admin_config has fields configured for the current action. If so, it uses those, if not, it tries to guess which attributes to use through the model’s “accessible_attributes”
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 238 def attributes return @attributes if @attributes model_klass = model_for controller fields = bootstrap_admin_config.send("#{params[:action]}_fields") || bootstrap_admin_config.send("action_fields") || find_attributes_for_class(model_klass) @attributes = fields.map do |att| BootstrapAdmin::Attribute.new att, model_klass.human_attribute_name(att), model_klass.type_of(att) end end |
#available_actions ⇒ Object
128 129 130 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 128 def available_actions bootstrap_admin_config.available_actions end |
#bootstrap_form_url(item) ⇒ Object
149 150 151 152 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 149 def bootstrap_form_url item item_name = item.class.name.underscore.gsub("/","_") [item, :as => item_name, :url => bootstrap_url_for(:action => (item.new_record? ? :create : :update), :id => item.id)] end |
#bootstrap_url_for(options = {}) ⇒ Object Also known as: bootstrap_url
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 133 def bootstrap_url_for = {} defaults = {:controller => params[:controller]}.merge ctrl_namespace = defaults[:controller].split("/").first # Fix for engines with shared namespace for some reason main_app.url_for doesn't find the route without this "/" # It consider the route inside of engine instead of main app. defaults[:controller] = "/#{defaults[:controller]}" if instance_eval "defined? #{ctrl_namespace}" instance_eval("#{ctrl_namespace}.method :url_for").call defaults else main_app.url_for(defaults) end end |
#collection_for(controller) ⇒ ActiveRecord::Base Array
211 212 213 214 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 211 def collection_for controller name = collection_name_for controller instance_variable_get "@#{name}" end |
#collection_name_for(controller) ⇒ String
224 225 226 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 224 def collection_name_for controller controller.class.name.sub("Controller", "").underscore.split('/').last end |
#display_field(item, field, &block) ⇒ Markup
Builds the markup to properly display a field, including label and field value.
If a block is given, then it uses the block as the field “value” else it looks at what the field value is and if it is:
* a boolean, builds a fake checkbox
* an array, builds a ul list with each element of the array
* else, just uses the string value
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 77 def display_field item, field, &block content_tag :p do content_tag(:b) do item.class.human_attribute_name(field) + ": " end + if block_given? capture(&block) else val = render_field item, field if val.class.name =~ /(TrueClass|FalseClass)/ content_tag(:span, :class => "checkbox #{val.to_s}") do content_tag(:span, :class=>"icon"){""} end elsif val.is_a? Array content_tag :ul do val.map do |uniq_val| content_tag(:li, uniq_val.to_s) end.join.html_safe end else val.to_s end end end.html_safe end |
#index_actions_for(item) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 155 def index_actions_for item actions = [] if available_actions.include? :show actions << link_to(:show, bootstrap_url_for(:action => :show, :id => item.id), class: 'btn') end if available_actions.include? :edit actions << link_to(:edit, bootstrap_url_for(:action => :edit, :id => item.id), class: 'btn') end if available_actions.include? :destroy actions << link_to(:destroy, bootstrap_url_for(:action => :show, :id => item.id), confirm: t(:confirm), method: :delete, class: 'btn btn-danger') end actions.join("\n").html_safe end |
#index_default_actions ⇒ Object
174 175 176 177 178 179 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 174 def index_default_actions if available_actions.include? :new model_klass = model_for controller link_to([:new, model_klass], bootstrap_url_for(:action => :new), :class => 'btn btn-primary').html_safe end end |
#link_to(*args, &block) ⇒ String
This re-implementation of link_to simply looks at the link label and if it falls under certain conditions, then it tries to create a label and/or url accordingly. Else works normally
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 22 def link_to(*args, &block) super(*args, &block) if block_given? # When args[0] is a symbol, then just translate the symbol... if args[0].is_a? Symbol super(t(args[0]), *args[1..-1]) # when arg[0] is a ActiveRecord class... elsif args[0].is_a?(Class) and args[0] < ActiveRecord::Base label = args[0].model_name.human.pluralize if args.length == 1 controller_name = args[0].name.underscore.pluralize super(label, url_for(controller: controller_name, action: "index")) else super(label, *args[1..-1]) end elsif args[0].is_a?(Array) && args[0][0].is_a?(Symbol) && args[0][1] < ActiveRecord::Base link_content = t("helpers.submit.#{args[0][0]}", :model => args[0][1].model_name.human) super(link_content, *args[1..-1]) else super(*args) end end |
#model_for(controller) ⇒ Class
194 195 196 197 198 199 200 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 194 def model_for controller if bootstrap_admin_config.model_name.blank? collection_name_for(controller).classify.constantize else bootstrap_admin_config.model_name.constantize end end |
#model_instance_for(controller) ⇒ ActiveRecord::Base
204 205 206 207 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 204 def model_instance_for controller name = collection_name_for(controller).singularize instance_variable_get "@#{name}" end |
#model_name_for(controller) ⇒ String
218 219 220 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 218 def model_name_for controller model_for(controller).model_name.to_s.underscore end |
#render_field(item, field) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 104 def render_field item, field if self.respond_to?(helper = "#{params[:action]}_#{field}") || self.respond_to?(helper = "#{field}") send helper, item else item.send field end end |
#render_form_field(form, field) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 114 def render_form_field form, field if self.respond_to?(helper = "#{params[:action]}_form_#{field.name}") || self.respond_to?(helper = "form_#{field.name}") send helper, form elsif field.association? form.association field.name else form.input field.name end end |
#render_with_fallback(*args) ⇒ Object
Tries to render the action/partial “namespaced” with the current action name if it fails, then simply calls render with whatever args that got passed in.
Ex:
# on get to '/admin/some_resource/new'
render_with_fallback 'form' # this will to render 'new_form', if it fails,
# then it will just call "render 'form'"
260 261 262 263 264 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 260 def render_with_fallback *args render "#{params[:action]}_#{args.first}", *args[1..-1] rescue ActionView::MissingTemplate render *args end |
#show_default_actions ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'app/helpers/bootstrap_admin_helper.rb', line 181 def show_default_actions model_instance = model_instance_for controller str = if available_actions.include? :edit link_to :edit, bootstrap_url_for(:action => :edit, :id => model_instance.id), :class => 'btn' end || "" str += link_to :back, bootstrap_url, :class => 'btn' str.html_safe end |