Module: Effective::FlashMessages
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/effective/flash_messages.rb
Instance Method Summary collapse
- #action_verb(action) ⇒ Object
-
#flash_danger(resource, action = nil, e: nil, name: nil) ⇒ Object
flash.now = flash_danger(@post).
-
#flash_errors(resource, e: nil) ⇒ Object
flash.now = “Unable to accept: #flash_errors(@post)”.
-
#flash_success(resource, action = nil, name: nil) ⇒ Object
flash = flash_success(@post).
Instance Method Details
#action_verb(action) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/controllers/concerns/effective/flash_messages.rb', line 62 def action_verb(action) action = action.to_s.gsub('_', ' ') word = action.split(' ').first.to_s if word == 'destroy' 'deleted' elsif word == 'undo' 'undid' elsif word == 'run' 'ran' elsif word.end_with?('e') action.sub(word, word + 'd') elsif ['a', 'i', 'o', 'u'].include?(word[-1]) action else action.sub(word, word + 'ed') end end |
#flash_danger(resource, action = nil, e: nil, name: nil) ⇒ Object
flash.now = flash_danger(@post)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/controllers/concerns/effective/flash_messages.rb', line 21 def flash_danger(resource, action = nil, e: nil, name: nil) raise 'expected an ActiveRecord resource' unless resource.respond_to?(:errors) && (name || resource.class.respond_to?(:model_name)) action ||= resource.respond_to?(:new_record?) ? (resource.new_record? ? :create : :update) : :save action = action.to_s.gsub('_', ' ') = flash_errors(resource, e: e) name ||= if resource.respond_to?(:destroyed?) && resource.destroyed? resource_human_name else resource.to_s.presence || resource_human_name end ["Unable to #{action}", (" #{name}" if name), (": #{}" if )].compact.join.html_safe end |
#flash_errors(resource, e: nil) ⇒ Object
flash.now = “Unable to accept: #flash_errors(@post)”
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/controllers/concerns/effective/flash_messages.rb', line 39 def flash_errors(resource, e: nil) raise 'expected an ActiveRecord resource' unless resource.respond_to?(:errors) = resource.errors.map do |error| attribute = error.respond_to?(:attribute) ? error.attribute : error = error.respond_to?(:attribute) ? error. : resource.errors[attribute].to_sentence if [0] == [0].upcase # If the error begins with a capital letter elsif attribute == :base elsif attribute.to_s.end_with?('_ids') "#{resource.class.human_attribute_name(attribute.to_s[0..-5].pluralize).downcase} #{}" else "#{resource.class.human_attribute_name(attribute).downcase} #{}" end end << e. if .blank? && e && e.respond_to?(:message) .uniq.to_sentence.presence end |
#flash_success(resource, action = nil, name: nil) ⇒ Object
flash = flash_success(@post)
8 9 10 11 12 13 14 15 16 17 18 |
# File 'app/controllers/concerns/effective/flash_messages.rb', line 8 def flash_success(resource, action = nil, name: nil) raise 'expected an ActiveRecord resource' unless (name || resource.class.respond_to?(:model_name)) name ||= if resource.respond_to?(:destroyed?) && resource.destroyed? resource_human_name else resource.to_s.presence || resource_human_name end "Successfully #{action_verb(action)} #{name || 'resource'}".html_safe end |