datewari 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf4bf4a79564aa3543d05ee1aa60a4f615726538
4
+ data.tar.gz: 89a58b9db6d661931f619365e3911c98b7b8b89d
5
+ SHA512:
6
+ metadata.gz: f4d2f499c621733fc876670d13116dea294f0c041e67267c93ac55744c7b904b10c63aa906719c7737b543dc7deb675c07c154bbb2428fb5c65b2bb0937efbb4
7
+ data.tar.gz: 90c4156ec06c15657e2099e4f835e68045e0ec162ffeb6168624a45504c97a081c2cdbc35a8a5b17788f7bdc5f592461bee932a400883b617d76ed8517a916aa
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ .bundle/
2
+ .yardoc
3
+ .project
4
+ Gemfile.lock
5
+ _yardoc/
6
+ coverage/
7
+ doc/
8
+ pkg/
9
+ spec/reports/
10
+ spec/dummy/config/database.yml
11
+ spec/dummy/db/schema.rb
12
+ spec/dummy/db/*.sqlite3
13
+ spec/dummy/log/*.log
14
+ spec/dummy/tmp/*
15
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ services:
6
+ - mysql
7
+ - postgresql
8
+ addons:
9
+ postgresql: "9.5"
10
+ env:
11
+ - DATABASE=mysql
12
+ - DATABASE=postgresql
13
+ gemfile:
14
+ - gemfiles/rails50.gemfile
15
+ - gemfiles/rails51.gemfile
16
+ before_script:
17
+ - cd spec/dummy
18
+ - bundle exec rake db:create db:migrate db:seed RAILS_ENV=test
19
+ - cd ../..
20
+ script: bundle exec rspec
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at kaneta@sitebridge.co.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.1.0"
4
+ gem "mysql2", "~> 0.4.4"
5
+ gem "pg", "~> 0.18"
6
+
7
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Datewari
2
+
3
+ An ActiveRecord extension to build date oriented pagination links such as monthly pages and weekly pages.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * rails 5.0+ (activerecord, activesupport, actionview)
9
+
10
+ Following ActiveRecord adapters are supported:
11
+
12
+ * mysql
13
+ * postgresql
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'datewari'
21
+ ```
22
+
23
+ Then execute:
24
+
25
+ $ bundle
26
+
27
+ ## Usage
28
+
29
+ Paginate collection by `created_at` column:
30
+
31
+ ```ruby
32
+ # yearly pagination
33
+ users = User.date_paginate(:created_at, :desc, date: '2018-01-01', scope: :yearly)
34
+
35
+ # monthly pagination
36
+ users = User.date_paginate(:created_at, :desc, date: '2018-01-01', scope: :monthly)
37
+
38
+ # weekly pagination
39
+ users = User.date_paginate(:created_at, :desc, date: '2018-01-01', scope: :weekly)
40
+
41
+ # daily pagination
42
+ users = User.date_paginate(:created_at, :desc, date: '2018-01-01', scope: :daily)
43
+ ```
44
+
45
+ ### Access paginator variables
46
+
47
+ You can get paginator variables as follows:
48
+
49
+ ```ruby
50
+ # dates of pages
51
+ users.paginator.pages
52
+
53
+ # count of total entries
54
+ users.paginator.total_entries
55
+ ```
56
+
57
+ ### Render pagination links
58
+
59
+ Render pagination links:
60
+
61
+ ```ruby
62
+ date_paginate users
63
+
64
+ # with options
65
+ date_paginate users, previous_label: 'Prev', next_label: 'Next'
66
+ ```
67
+
68
+ Available options:
69
+
70
+ * `previous_label`: label for previous link.
71
+ * `next_label`: label for next link.
72
+ * `page_gap`: label for abbrebiated pages.
73
+ * `link_separator`: label between pages.
74
+ * `yearly_format`: date format for yearly pagination.
75
+ * `monthly_format`: date format for monthly pagination.
76
+ * `weekly_format`: date format for weekly pagination.
77
+ * `daily_format`: date format for daily pagination.
78
+ * `inner_window`: window size around current page link. default: `4`.
79
+ * `outer_window`: window size around first page and last page link. default: `1`.
80
+ * `page_links`: render page links or not. default: `true`.
81
+ * `param_name`: query parameter name. default: `date`.
82
+ * `params`: optional parameters for page links.
83
+ * `renderer`: custom link render class.
84
+
85
+ ### Render page info
86
+
87
+ Render page info:
88
+
89
+ ```ruby
90
+ date_page_entries_info users
91
+ ```
92
+
93
+ ### I18n
94
+
95
+ I18n default values are as follows:
96
+
97
+ ```yaml
98
+ en:
99
+ date_paginate:
100
+ previous_label: "← Previous"
101
+ next_label: "Next →"
102
+ page_gap: "…"
103
+ link_separator: "|"
104
+ yearly_format: "%Y"
105
+ monthly_format: "%Y-%m"
106
+ weekly_format: "%Y-%m-%d"
107
+ daily_format: "%Y-%m-%d"
108
+ page_entries_info:
109
+ single_page:
110
+ zero: "No items found"
111
+ other: "Displaying <b>%{total}</b> items"
112
+ multi_page: "Displaying <b>%{current}</b> of <b>%{total}</b> in total"
113
+ ```
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome at https://github.com/kanety/datewari.
118
+
119
+ ## License
120
+
121
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/datewari.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'datewari/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "datewari"
8
+ spec.version = Datewari::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{An ActiveRecord extension to build date oriented pagination links}
12
+ spec.description = %q{An ActiveRecord extension to build date oriented pagination links such as monthly pages and weekly pages}
13
+ spec.homepage = "https://github.com/kanety/datewari"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activerecord", ">= 5.0"
21
+ spec.add_dependency "activesupport", ">= 5.0"
22
+ spec.add_dependency "actionview", ">= 5.0"
23
+
24
+ spec.add_development_dependency "rails", ">= 5.0"
25
+ spec.add_development_dependency "mysql2"
26
+ spec.add_development_dependency "pg"
27
+ spec.add_development_dependency "rspec-rails"
28
+ spec.add_development_dependency "rails-controller-testing"
29
+ spec.add_development_dependency "simplecov"
30
+ spec.add_development_dependency "pry-rails"
31
+ spec.add_development_dependency "pry-byebug"
32
+ end
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.0.0"
4
+ gem "mysql2", "~> 0.4.4"
5
+ gem "pg", "~> 0.18"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 5.1.0"
4
+ gem "mysql2", "~> 0.4.4"
5
+ gem "pg", "~> 0.18"
6
+
7
+ gemspec path: "../"
data/lib/datewari.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'datewari/version'
2
+ require 'datewari/paginator'
3
+ require 'datewari/helper'
4
+ require 'datewari/util'
5
+ require 'datewari/railtie' if defined?(Rails)
@@ -0,0 +1,40 @@
1
+ require_relative 'helper/link_renderer'
2
+ require_relative 'helper/parts_builder'
3
+
4
+ module Datewari
5
+ module Helper
6
+ I18N_KEY = 'date_paginate'
7
+
8
+ def date_paginate(rel, config = {})
9
+ [:previous_label, :next_label, :page_gap, :link_separator,
10
+ :yearly_format, :monthly_format, :weekly_format, :daily_format].each do |key|
11
+ config[key] ||= I18n.t("#{I18N_KEY}.#{key}").html_safe
12
+ end
13
+
14
+ config[:page_links] = true unless config.key?(:page_links)
15
+ config[:inner_window] ||= 4
16
+ config[:outer_window] ||= 1
17
+ config[:param_name] ||= :date
18
+ config[:params] ||= {}
19
+
20
+ renderer = config[:renderer] || LinkRenderer
21
+ renderer.new(config, rel.paginator, self).render
22
+ end
23
+
24
+ def date_page_entries_info(rel)
25
+ paginator = rel.paginator
26
+ i18n_key = "#{I18N_KEY}.page_entries_info"
27
+
28
+ content_tag :div, class: 'pageEntryInfo' do
29
+ case paginator.pages.size
30
+ when 0
31
+ I18n.t("#{i18n_key}.single_page.zero").html_safe
32
+ when 1
33
+ I18n.t("#{i18n_key}.single_page.other", total: paginator.total_entries).html_safe
34
+ else
35
+ I18n.t("#{i18n_key}.multi_page", current: paginator.current_entries, total: paginator.total_entries).html_safe
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,101 @@
1
+ module Datewari
2
+ module Helper
3
+ class LinkRenderer
4
+ def initialize(config, paginator, template)
5
+ @config = config
6
+ @paginator = paginator
7
+ @template = template
8
+ end
9
+
10
+ def render
11
+ return '' if @paginator.pages.size < 2
12
+
13
+ container do
14
+ parts = PartsBuilder.new(@config, @paginator).build
15
+ parts.map { |part|
16
+ case part
17
+ when :prev
18
+ prev_link
19
+ when :next
20
+ next_link
21
+ when :gap
22
+ gap if @config[:page_links]
23
+ else
24
+ date_link(part) if @config[:page_links]
25
+ end
26
+ }.compact.join(separator).html_safe
27
+ end
28
+ end
29
+
30
+ def container
31
+ content_tag :div, class: 'pagination' do
32
+ yield
33
+ end
34
+ end
35
+
36
+ def prev_link
37
+ if (date = @paginator.prev_date)
38
+ link_to @config[:previous_label], url(date), class: 'previous_page', rel: 'prev'
39
+ else
40
+ content_tag :span, @config[:previous_label], class: 'previous_page disabled'
41
+ end
42
+ end
43
+
44
+ def next_link
45
+ if (date = @paginator.next_date)
46
+ link_to @config[:next_label], url(date), class: 'next_page', rel: 'next'
47
+ else
48
+ content_tag :span, @config[:next_label], class: 'next_page disabled'
49
+ end
50
+ end
51
+
52
+ def date_link(date)
53
+ if date == @paginator.current_date
54
+ content_tag :em, date_label(date, @paginator.scope), class: 'current'
55
+ else
56
+ link_to date_label(date, @paginator.scope), url(date)
57
+ end
58
+ end
59
+
60
+ def gap
61
+ content_tag :span, @config[:page_gap], class: 'gap'
62
+ end
63
+
64
+ def separator
65
+ content_tag :span, @config[:link_separator], class: 'separator'
66
+ end
67
+
68
+ def date_label(date, scope)
69
+ date.strftime(@config["#{scope}_format".to_sym])
70
+ end
71
+
72
+ def date_param(date)
73
+ date.strftime('%Y-%m-%d')
74
+ end
75
+
76
+ def url(date)
77
+ date_param = if date == @paginator.pages.first
78
+ nil
79
+ else
80
+ date_param(date)
81
+ end
82
+ request_params = @template.request.params.except(:controller, :action)
83
+ url_for(request_params.merge(@config[:params]).merge(@config[:param_name] => date_param))
84
+ end
85
+
86
+ private
87
+
88
+ def url_for(*args, &block)
89
+ @template.url_for(*args, &block)
90
+ end
91
+
92
+ def content_tag(*args, &block)
93
+ @template.content_tag(*args, &block)
94
+ end
95
+
96
+ def link_to(*args, &block)
97
+ @template.link_to(*args, &block)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,82 @@
1
+ module Datewari
2
+ module Helper
3
+ class PartsBuilder
4
+ def initialize(config, paginator)
5
+ @inner = config[:inner_window]
6
+ @outer = config[:outer_window]
7
+ @paginator = paginator
8
+ end
9
+
10
+ def build
11
+ pages = @paginator.pages
12
+ index = @paginator.current_index
13
+ lefts, rights, centers = page_indices(pages, index)
14
+
15
+ parts = [:prev]
16
+
17
+ if lefts.size > 0 && centers.size > 0 && rights.size > 0 && lefts.last + 1 < centers.first && centers.last < rights.first - 1
18
+ parts += lefts
19
+ parts << :gap
20
+ parts += centers
21
+ parts << :gap
22
+ parts += rights
23
+ elsif centers.size > 0 && rights.size > 0 && centers.last < rights.first - 1
24
+ parts += (lefts + centers).uniq
25
+ parts << :gap
26
+ parts += rights
27
+ elsif lefts.size > 0 && centers.size > 0 && lefts.last + 1 < centers.first
28
+ parts += lefts
29
+ parts << :gap
30
+ parts += (centers + rights).uniq
31
+ else
32
+ parts += (lefts + centers + rights).uniq
33
+ end
34
+
35
+ parts << :next
36
+
37
+ parts.map { |part| part.is_a?(Integer) ? pages[part] : part }.compact
38
+ end
39
+
40
+ private
41
+
42
+ def page_indices(pages, index)
43
+ max = pages.size - 1
44
+ return left_indices(@outer, max), right_indices(@outer, max), center_indices(@inner, index, max)
45
+ end
46
+
47
+ def left_indices(outer, max)
48
+ if outer > 0
49
+ (0 .. range(outer - 1, max)).to_a
50
+ else
51
+ []
52
+ end
53
+ end
54
+
55
+ def right_indices(outer, max)
56
+ if outer > 0
57
+ (range(max - outer + 1, max) .. max).to_a
58
+ else
59
+ []
60
+ end
61
+ end
62
+
63
+ def center_indices(inner, index, max)
64
+ if inner >= 0
65
+ (range(index - inner, max) .. range(index + inner, max)).to_a
66
+ else
67
+ []
68
+ end
69
+ end
70
+
71
+ def range(value, max)
72
+ if value < 0
73
+ 0
74
+ elsif value > max
75
+ max
76
+ else
77
+ value
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,15 @@
1
+ en:
2
+ date_paginate:
3
+ previous_label: "&#8592; Previous"
4
+ next_label: "Next &#8594;"
5
+ page_gap: "&hellip;"
6
+ link_separator: "|"
7
+ yearly_format: "%Y"
8
+ monthly_format: "%Y-%m"
9
+ weekly_format: "%Y-%m-%d"
10
+ daily_format: "%Y-%m-%d"
11
+ page_entries_info:
12
+ single_page:
13
+ zero: "No items found"
14
+ other: "Displaying <b>%{total}</b> items"
15
+ multi_page: "Displaying <b>%{current}</b> of <b>%{total}</b> in total"
@@ -0,0 +1,15 @@
1
+ ja:
2
+ date_paginate:
3
+ previous_label: "前へ"
4
+ next_label: "次へ"
5
+ page_gap: "&hellip;"
6
+ link_separator: "|"
7
+ yearly_format: "%Y"
8
+ monthly_format: "%Y-%m"
9
+ weekly_format: "%Y-%m-%d"
10
+ daily_format: "%Y-%m-%d"
11
+ page_entries_info:
12
+ single_page:
13
+ zero: "<b>0</b> 件"
14
+ other: "<b>%{total}</b> 件"
15
+ multi_page: "<b>%{total}</b> 件中 <b>%{current}</b> 件を表示"
@@ -0,0 +1,110 @@
1
+ require_relative 'paginator/base'
2
+
3
+ module Datewari
4
+ module RelationMethods
5
+ attr_accessor :paginator
6
+ end
7
+
8
+ module Extension
9
+ def date_paginate(column, order, options = {})
10
+ options[:scope] ||= :monthly
11
+
12
+ rel = self.all
13
+ paginator = Paginator.new(rel, column, order, options)
14
+
15
+ rel = paginator.paginate
16
+ rel = rel.extending RelationMethods
17
+ rel.paginator = paginator
18
+ rel
19
+ end
20
+ end
21
+
22
+ class Paginator
23
+ attr_accessor :scope, :date
24
+
25
+ def initialize(rel, column, order, options)
26
+ @scope = options[:scope].to_sym
27
+
28
+ @query = case rel.klass.connection.adapter_name
29
+ when 'PostgreSQL'
30
+ require_relative 'paginator/postgresql'
31
+ Postgresql.new(rel, column, order, options)
32
+ when 'Mysql2'
33
+ require_relative 'paginator/mysql'
34
+ Mysql.new(rel, column, order, options)
35
+ else
36
+ raise ArgumentError.new("Unsupported adapter: #{rel.klass.connection.adapter_name}")
37
+ end
38
+
39
+ @date = Util.parse_date(options[:date]) || pages.first || Date.today
40
+ end
41
+
42
+ def pages
43
+ @pages ||= @query.pages
44
+ end
45
+
46
+ def paginate
47
+ @query.paginate(*date_range)
48
+ end
49
+
50
+ def total_entries
51
+ @total_entries ||= @query.total_entries
52
+ end
53
+
54
+ def current_entries
55
+ @current_entries ||= paginate.count
56
+ end
57
+
58
+ def date_range
59
+ @date_range ||= send("date_range_for_#{@scope}")
60
+ end
61
+
62
+ def current_index
63
+ @current_index ||= if pages.empty?
64
+ 0
65
+ elsif (i = pages.index(date_range.first.to_date))
66
+ i
67
+ else
68
+ pages.size - 1
69
+ end
70
+ end
71
+
72
+ def current_date
73
+ pages[current_index]
74
+ end
75
+
76
+ def prev_date
77
+ if current_index > 0
78
+ pages[current_index - 1]
79
+ else
80
+ nil
81
+ end
82
+ end
83
+
84
+ def next_date
85
+ if current_index < pages.size - 1
86
+ pages[current_index + 1]
87
+ else
88
+ nil
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def date_range_for_yearly
95
+ [@date.to_time.beginning_of_year, @date.to_time.end_of_year]
96
+ end
97
+
98
+ def date_range_for_monthly
99
+ [@date.to_time.beginning_of_month, @date.to_time.end_of_month]
100
+ end
101
+
102
+ def date_range_for_weekly
103
+ [@date.to_time.beginning_of_week, @date.to_time.end_of_week]
104
+ end
105
+
106
+ def date_range_for_daily
107
+ [@date.to_time.beginning_of_day, @date.to_time.end_of_day]
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,67 @@
1
+ module Datewari
2
+ class Paginator
3
+ class Base
4
+ class_attribute :cast_function, :date_format
5
+
6
+ def initialize(rel, column, order, options)
7
+ @rel = rel
8
+ @column = column
9
+ @order = order
10
+ @scope = options[:scope]
11
+ end
12
+
13
+ def pages
14
+ send("pages_for_#{@scope}")
15
+ end
16
+
17
+ def paginate(start_date, end_date)
18
+ @rel.where("#{quoted_column} BETWEEN ? AND ?", start_date, end_date)
19
+ .order("#{quoted_column} #{@order.to_s.upcase}")
20
+ end
21
+
22
+ def total_entries
23
+ @rel.count
24
+ end
25
+
26
+ private
27
+
28
+ def quoted_column
29
+ table_column = if @column.to_s.include?('.')
30
+ @column
31
+ else
32
+ "#{@rel.klass.table_name}.#{@column}"
33
+ end
34
+ @rel.klass.connection.quote_table_name(table_column)
35
+ end
36
+
37
+ def pages_for_yearly
38
+ dates = pluck_dates(self.class.cast_function, self.class.date_format[:yearly])
39
+ dates.map { |date| Date.parse(date) }
40
+ end
41
+
42
+ def pages_for_monthly
43
+ dates = pluck_dates(self.class.cast_function, self.class.date_format[:monthly])
44
+ dates.map { |date| Date.parse(date) }
45
+ end
46
+
47
+ def pages_for_weekly
48
+ dates = pluck_dates(self.class.cast_function, self.class.date_format[:weekly])
49
+ dates.map { |date| Date.commercial(*date.split('-').map(&:to_i)) }
50
+ end
51
+
52
+ def pages_for_daily
53
+ dates = pluck_dates(self.class.cast_function, self.class.date_format[:daily])
54
+ dates.map { |date| Date.parse(date) }
55
+ end
56
+
57
+ def pluck_dates(function, format)
58
+ date_sql = "#{function}(#{quoted_column}, '#{format}')"
59
+ @rel.limit(nil)
60
+ .offset(nil)
61
+ .group(date_sql)
62
+ .reorder("#{date_sql} #{@order}")
63
+ .pluck(date_sql)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,11 @@
1
+ module Datewari
2
+ class Paginator
3
+ class Mysql < Base
4
+ self.cast_function = 'DATE_FORMAT'
5
+ self.date_format = { yearly: '%Y-%m-01',
6
+ monthly: '%Y-%m-01',
7
+ weekly: '%x-%v',
8
+ daily: '%Y-%m-%d' }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Datewari
2
+ class Paginator
3
+ class Postgresql < Base
4
+ self.cast_function = 'TO_CHAR'
5
+ self.date_format = { yearly: 'YYYY-01-01',
6
+ monthly: 'YYYY-MM-01',
7
+ weekly: 'IYYY-IW',
8
+ daily: 'YYYY-MM-DD' }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Datewari
2
+ class Railtie < Rails::Railtie
3
+ ActiveSupport.on_load :active_record do
4
+ ActiveRecord::Base.send :extend, Extension
5
+ ActiveRecord::Relation.send :include, Extension
6
+ end
7
+
8
+ ActiveSupport.on_load :action_view do
9
+ ActionView::Base.send :include, Helper
10
+ end
11
+
12
+ ActiveSupport.on_load :i18n do
13
+ I18n.load_path += Dir["#{File.expand_path('../locales', __FILE__)}/*.yml"]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Datewari
2
+ class Util
3
+ class << self
4
+ def parse_date(date)
5
+ if date.is_a?(String)
6
+ Date.parse(date)
7
+ else
8
+ date
9
+ end
10
+ rescue => e
11
+ nil
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Datewari
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,222 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datewari
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionview
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails-controller-testing
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: An ActiveRecord extension to build date oriented pagination links such
168
+ as monthly pages and weekly pages
169
+ email:
170
+ - kaneta@sitebridge.co.jp
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".travis.yml"
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - LICENSE
181
+ - README.md
182
+ - Rakefile
183
+ - datewari.gemspec
184
+ - gemfiles/rails50.gemfile
185
+ - gemfiles/rails51.gemfile
186
+ - lib/datewari.rb
187
+ - lib/datewari/helper.rb
188
+ - lib/datewari/helper/link_renderer.rb
189
+ - lib/datewari/helper/parts_builder.rb
190
+ - lib/datewari/locales/en.yml
191
+ - lib/datewari/locales/ja.yml
192
+ - lib/datewari/paginator.rb
193
+ - lib/datewari/paginator/base.rb
194
+ - lib/datewari/paginator/mysql.rb
195
+ - lib/datewari/paginator/postgresql.rb
196
+ - lib/datewari/railtie.rb
197
+ - lib/datewari/util.rb
198
+ - lib/datewari/version.rb
199
+ homepage: https://github.com/kanety/datewari
200
+ licenses: []
201
+ metadata: {}
202
+ post_install_message:
203
+ rdoc_options: []
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubyforge_project:
218
+ rubygems_version: 2.5.2.2
219
+ signing_key:
220
+ specification_version: 4
221
+ summary: An ActiveRecord extension to build date oriented pagination links
222
+ test_files: []