e9_rails 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/lib/e9_rails/active_record/attribute_searchable.rb +23 -0
- data/lib/e9_rails/active_record/inheritable_options.rb +58 -0
- data/lib/e9_rails/active_record/sti.rb +29 -0
- data/lib/e9_rails/helpers/pagination.rb +57 -0
- data/lib/e9_rails/helpers/title.rb +43 -0
- data/lib/e9_rails/version.rb +1 -1
- data/lib/e9_rails.rb +8 -0
- metadata +8 -3
data/.gitignore
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
module E9Rails::ActiveRecord
|
2
|
+
module AttributeSearchable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
scope :attr_like, lambda {|*args| where(attr_like_scope_condition(*args)) }
|
7
|
+
scope :any_attrs_like, lambda {|*args| where(any_attrs_like_scope_conditions(*args)) }
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def attr_like_scope_condition(attr_name, string, opts = {})
|
12
|
+
matcher = opts.delete(:matcher) || "%%%s%%"
|
13
|
+
arel_table[attr_name].matches(matcher % string)
|
14
|
+
end
|
15
|
+
|
16
|
+
def any_attrs_like_scope_conditions(*args)
|
17
|
+
opts = args.extract_options!
|
18
|
+
string = args.pop
|
19
|
+
args.flatten.map {|attr_name| attr_like_scope_condition(attr_name, string, opts) }.inject(&:or)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_model/naming'
|
2
|
+
require 'active_model/translation'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
|
5
|
+
module E9Rails::ActiveRecord
|
6
|
+
module InheritableOptions
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
serialize :options
|
11
|
+
|
12
|
+
class_inheritable_accessor :options_parameters
|
13
|
+
self.options_parameters = []
|
14
|
+
|
15
|
+
class_inheritable_accessor :options_class
|
16
|
+
self.options_class = Options
|
17
|
+
|
18
|
+
self.options_class.lookup_ancestors = lookup_ancestors
|
19
|
+
end
|
20
|
+
|
21
|
+
def options=(hash={})
|
22
|
+
write_attribute(:options, hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
def options
|
26
|
+
self.class.options_class.new( (read_attribute(:options) || {}).reverse_merge(Hash[options_parameters.zip([nil])]), self)
|
27
|
+
end
|
28
|
+
|
29
|
+
class Options < HashWithIndifferentAccess
|
30
|
+
extend ActiveModel::Naming
|
31
|
+
extend ActiveModel::Translation
|
32
|
+
|
33
|
+
class_inheritable_accessor :lookup_ancestors
|
34
|
+
|
35
|
+
attr_reader :base
|
36
|
+
|
37
|
+
class << self
|
38
|
+
def name; 'Options' end
|
39
|
+
def i18n_scope; :activerecord end
|
40
|
+
end
|
41
|
+
|
42
|
+
def initialize(hash, base)
|
43
|
+
merge!(hash)
|
44
|
+
@base = base
|
45
|
+
class << self; self; end.class_eval do
|
46
|
+
hash.each do |k, v|
|
47
|
+
define_method(k) { self[k] }
|
48
|
+
define_method("#{k}=") do |v|
|
49
|
+
self[k] = v
|
50
|
+
@base.options = Hash[self]
|
51
|
+
self[k]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module E9Rails::ActiveRecord
|
2
|
+
module STI
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
scope :of_type, lambda {|*types| where(:type => types.flatten.map {|t| t.to_s.classify }) }
|
7
|
+
scope :not_of_type, lambda {|*types| where(arel_table[:type].in(types.flatten.map {|t| t.to_s.classify }).not) }
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def subclasses
|
12
|
+
@_subclasses ||= begin
|
13
|
+
# TODO is there a simpler, existing way of finding subclass types with STI
|
14
|
+
|
15
|
+
# TODO make this work with the new arel rewrite (#map is no longer a method)
|
16
|
+
#klasses = arel_table.project(Arel::Distinct.new(arel_table[:type])).map {|r| r.tuple.first }
|
17
|
+
klasses = connection.select_values("select distinct(type) from #{table_name}")
|
18
|
+
klasses.map! {|k| k.constantize rescue next }
|
19
|
+
klasses.compact!
|
20
|
+
klasses
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def subclasses_with_ancestor(mod)
|
25
|
+
subclasses.select {|klass| klass.ancestors.include?(mod) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module E9Rails::Helpers
|
2
|
+
module Pagination
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_inheritable_accessor :pagination_page_param
|
7
|
+
self.pagination_page_param = :page
|
8
|
+
|
9
|
+
class_inheritable_accessor :pagination_per_page_param
|
10
|
+
self.pagination_per_page_param = :per_page
|
11
|
+
|
12
|
+
class_inheritable_accessor :pagination_per_page_default
|
13
|
+
self.pagination_per_page_default = 10
|
14
|
+
|
15
|
+
class_inheritable_accessor :pagination_feed_per_page_default
|
16
|
+
self.pagination_feed_per_page_default = 50
|
17
|
+
|
18
|
+
before_filter :pagination_parameters
|
19
|
+
|
20
|
+
helper_method :paging_page, :pagination_per_page, :paging?
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def pagination_per_page
|
26
|
+
if request.format.to_s =~ /rss/
|
27
|
+
self.class.pagination_feed_per_page_default
|
28
|
+
else
|
29
|
+
self.class.pagination_per_page_default
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def pagination_parameters
|
34
|
+
@pagination_parameters ||= pagination_defaults.dup.tap do |opts|
|
35
|
+
if page = params[self.class.pagination_page_param]
|
36
|
+
opts[self.class.pagination_page_param] = page
|
37
|
+
end
|
38
|
+
|
39
|
+
if per_page = params.delete(self.class.pagination_per_page_param)
|
40
|
+
opts[self.class.pagination_per_page_param] = per_page
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def paging_page
|
46
|
+
pagination_parameters[self.class.pagination_page_param] || 1
|
47
|
+
end
|
48
|
+
|
49
|
+
def paging?
|
50
|
+
!!params[self.class.pagination_page_param]
|
51
|
+
end
|
52
|
+
|
53
|
+
def pagination_defaults
|
54
|
+
{ self.class.pagination_page_param => 1, self.class.pagination_per_page_param => pagination_per_page }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module E9Rails::Helpers
|
2
|
+
module Title
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper HelperMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module HelperMethods
|
10
|
+
def meta_title(title = nil, options = {})
|
11
|
+
[
|
12
|
+
title || @_title,
|
13
|
+
respond_to?(:site_name) ? send(:site_name) : nil
|
14
|
+
].flatten.compact.map {|t| sanitize(t) }.join(options[:delimiter] || ' - ').html_safe
|
15
|
+
end
|
16
|
+
|
17
|
+
def title(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
|
20
|
+
if !args.empty?
|
21
|
+
@_title = args.dup
|
22
|
+
base_title = sanitize(args.shift)
|
23
|
+
|
24
|
+
options[:class] = [options[:class], 'title'].compact.join(' ')
|
25
|
+
options[:class].concat(' error') if options[:error]
|
26
|
+
|
27
|
+
unless options[:hide_title]
|
28
|
+
content = base_title
|
29
|
+
content = content_tag(options[:inner_tag], content) if options[:inner_tag]
|
30
|
+
|
31
|
+
content_tag(:h1, options.slice(:id, :class)) do
|
32
|
+
''.tap do |html|
|
33
|
+
html.concat options[:prepend] if options[:prepend]
|
34
|
+
html.concat content
|
35
|
+
html.concat options[:append] if options[:append]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/e9_rails/version.rb
CHANGED
data/lib/e9_rails.rb
CHANGED
@@ -6,5 +6,13 @@ module E9Rails
|
|
6
6
|
module Helpers
|
7
7
|
autoload :Translation, 'e9_rails/helpers/translation'
|
8
8
|
autoload :ResourceErrorMessages, 'e9_rails/helpers/resource_error_messages'
|
9
|
+
autoload :Title, 'e9_rails/helpers/title'
|
10
|
+
autoload :Pagination, 'e9_rails/helpers/pagination'
|
11
|
+
end
|
12
|
+
|
13
|
+
module ActiveRecord
|
14
|
+
autoload :STI, 'e9_rails/active_record/sti'
|
15
|
+
autoload :AttributeSearchable, 'e9_rails/active_record/attribute_searchable'
|
16
|
+
autoload :InheritableOptions, 'e9_rails/active_record/inheritable_options'
|
9
17
|
end
|
10
18
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 5
|
9
|
+
version: 0.0.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Cox
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-03-
|
17
|
+
date: 2011-03-25 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -61,7 +61,12 @@ files:
|
|
61
61
|
- Rakefile
|
62
62
|
- e9_rails.gemspec
|
63
63
|
- lib/e9_rails.rb
|
64
|
+
- lib/e9_rails/active_record/attribute_searchable.rb
|
65
|
+
- lib/e9_rails/active_record/inheritable_options.rb
|
66
|
+
- lib/e9_rails/active_record/sti.rb
|
67
|
+
- lib/e9_rails/helpers/pagination.rb
|
64
68
|
- lib/e9_rails/helpers/resource_error_messages.rb
|
69
|
+
- lib/e9_rails/helpers/title.rb
|
65
70
|
- lib/e9_rails/helpers/translation.rb
|
66
71
|
- lib/e9_rails/version.rb
|
67
72
|
has_rdoc: true
|