nifty-utils 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ require 'nifty/utils/version'
2
+ require 'nifty/utils/railtie' if defined?(Rails)
@@ -0,0 +1,38 @@
1
+ module Nifty
2
+ module Utils
3
+ module ActiveRecord
4
+
5
+ #:nodoc:
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ # Allows you to automatically create inquiry methods for string field. For example, if you have
13
+ # an Order model which has a status field containing 'approved' or 'delivered' you may wish to
14
+ # have a #approved? or #delivered? method on the model.
15
+ #
16
+ # class Order < ActiveRecord::Baser
17
+ # STATUSES = ['approved', 'delivered']
18
+ # inquirer :status, *STATUSES
19
+ # end
20
+ #
21
+ # order = Order.new(:status => 'approved')
22
+ # order.approved? #=> true
23
+ # order.delivered? #=> false
24
+ #
25
+ #
26
+ def inquirer(field, *options)
27
+ options.each do |option|
28
+ define_method "#{option}?" do
29
+ self.read_attribute(field).to_s == option.to_s
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,23 @@
1
+ module Nifty
2
+ module Utils
3
+ class Railtie < Rails::Railtie #:nodoc:
4
+
5
+ initializer 'nifty.utils.initialize' do
6
+
7
+ # Load the Active Record extensions
8
+ ActiveSupport.on_load(:active_record) do
9
+ require 'nifty/utils/active_record'
10
+ ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord
11
+ end
12
+
13
+ # load the Action View helpers
14
+ ActiveSupport.on_load(:action_view) do
15
+ require 'nifty/utils/view_helpers'
16
+ ActionView::Base.send :include, Nifty::Utils::ViewHelpers
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Nifty
2
+ module Utils
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ module Nifty
2
+ module Utils
3
+ module ViewHelpers
4
+
5
+ # Displays the full contents of the `flash` hash within an appropriate <div>
6
+ # element. The ID of the outputted div will be `flash-alert` where alert is the
7
+ # type of flash.
8
+ #
9
+ # If there are multiple flashes set, they will all be displayed.
10
+ def display_flash
11
+ flashes = flash.collect do |key,msg|
12
+ content_tag :div, content_tag(:p, h(msg)), :id => "flash-#{key}"
13
+ end.join.html_safe
14
+ end
15
+
16
+ # Renders an `<img>` containing a link to the gravatar for the given e-mail address.
17
+ # Available options as follows:
18
+ #
19
+ # * <tt>:size</tt>: the size in pixels of the outputted gravatar. Defaults to 35.
20
+ # * <tt>:default</tt>: the gravatar to fallback to if the user has no gravatar
21
+ # (see gravatar for available options or pass a URL). Defaults to 'identicon'.
22
+ # * <tt>:rating</tt>: the maximum rating to use. Defaults to PG.
23
+ # * <tt>:class</tt>: the value for the class attribute for the outputted img tag.
24
+ # Defaults to 'gravatar'.
25
+ # * <tt>:secure</tt>: wherher or not to output an HTTPS version of the gravatar or not.
26
+ # Defaults to the value of `request.ssl?`.
27
+ def gravatar(email, options = {})
28
+ options[:size] ||= 35
29
+ options[:default] ||= 'identicon'
30
+ options[:rating] ||= 'PG'
31
+ options[:class] ||= 'gravatar'
32
+ options[:secure] ||= request.ssl?
33
+ host = (options[:secure] ? 'https://secure.gravatar.com' : 'http://gravatar.com')
34
+ path = "/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email)}&rating=#{options[:rating]}&size=#{options[:size] * 2}&d=#{options[:default]}"
35
+ image_tag([host,path].join, :class => options[:class], :width => options[:size], :height => options[:size])
36
+ end
37
+
38
+ # Renders a tick or cross character based on the provided boolean. Additional options
39
+ # can be passed if needed.
40
+ #
41
+ # * <tt>:true_text</tt> - text to display next to a tick
42
+ # * <tt>:false_text</tt> - text to display next to a cross
43
+ def boolean_tag(bool, tip = nil, options = {})
44
+ true_text, false_text = "", ""
45
+ true_text = " <b>#{options[:true_text]}</b>" if options[:true_text]
46
+ false_text = " <b>#{options[:false_text]}</b>" if options[:false_text]
47
+ content_tag :span, (bool ? "<span class='true'>&#x2714;#{true_text}</span>" : "<span class='false'>&#x2718;#{false_text}</span>").html_safe, :class => "boolean", :title => tip
48
+ end
49
+
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nifty-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Cooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A set of useful utilties for Rails applications
15
+ email:
16
+ - adam@niftyware.io
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/nifty/utils/active_record.rb
22
+ - lib/nifty/utils/railtie.rb
23
+ - lib/nifty/utils/version.rb
24
+ - lib/nifty/utils/view_helpers.rb
25
+ - lib/nifty/utils.rb
26
+ homepage: https://github.com/niftyware/utils
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.23
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: A set of useful utilties for Rails applications
50
+ test_files: []