active_record_florder 0.0.1

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: 11ac613fa7e6033d57f64ea0683b2be779b6a867
4
+ data.tar.gz: 67b8618a3afef2a25282ba300fd618419a93d790
5
+ SHA512:
6
+ metadata.gz: 8a3ad68f6039ea985554d101393ef0f84cf6fe122ae74c32190a130a4dbe407f260fe6ac3ad71ec88c96bda8e6d99dadc48e9e2a5ae1e05a23d2b1bb53533d65
7
+ data.tar.gz: 2d6e20bc6515dde13191cac087547d7f96abf7e6920bd50cd09d80e5297f55304c586d0e56aef9edcebc604c6cee125e04a1fd192b9b2ea5672758395f7dac28
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ root = true
4
+
5
+ [*]
6
+ end_of_line = lf
7
+ insert_final_newline = true
8
+ indent_style = space
9
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ /db/config.yml
2
+ /.bundle/
3
+
4
+ *.sqlite3
5
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.3
data/.travis.yml ADDED
@@ -0,0 +1,29 @@
1
+ language: ruby
2
+
3
+ sudo: false
4
+
5
+ rvm:
6
+ - 2.0.0
7
+ - 2.1
8
+ - 2.2.3
9
+ - 2.3.0
10
+ - ruby-head
11
+
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: ruby-head
15
+ fast_finish: true
16
+
17
+ install: bundle install --path=vendor/bundle --retry=3 --jobs=3
18
+
19
+ before_script:
20
+ - cp db/config.travis.yml db/config.yml
21
+
22
+ script:
23
+ - bundle exec rake db:migrate --trace
24
+ - bundle exec rake db:test:prepare
25
+ - bundle exec rake
26
+
27
+ addons:
28
+ code_climate:
29
+ repo_token: 79db524674654c4815f1923c6d5384922226942410c2fa25c5e5cbf9f3f7b372
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # A sample Gemfile
2
+ source "https://rubygems.org"
3
+
4
+ gem 'activesupport', '~> 4.0'
5
+ gem 'activerecord', '~> 4.0.0'
6
+
7
+ group :test do
8
+ gem 'rake'
9
+ gem 'sqlite3'
10
+ gem 'active_record_tasks', '~> 1.1.0'
11
+ gem 'rspec'
12
+ gem 'codeclimate-test-reporter', require: false
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ active_record_tasks (1.1.0)
5
+ activerecord (>= 4.0)
6
+ rainbow
7
+ rake
8
+ activemodel (4.0.13)
9
+ activesupport (= 4.0.13)
10
+ builder (~> 3.1.0)
11
+ activerecord (4.0.13)
12
+ activemodel (= 4.0.13)
13
+ activerecord-deprecated_finders (~> 1.0.2)
14
+ activesupport (= 4.0.13)
15
+ arel (~> 4.0.0)
16
+ activerecord-deprecated_finders (1.0.4)
17
+ activesupport (4.0.13)
18
+ i18n (~> 0.6, >= 0.6.9)
19
+ minitest (~> 4.2)
20
+ multi_json (~> 1.3)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 0.3.37)
23
+ arel (4.0.2)
24
+ builder (3.1.4)
25
+ codeclimate-test-reporter (0.4.8)
26
+ simplecov (>= 0.7.1, < 1.0.0)
27
+ diff-lcs (1.2.5)
28
+ docile (1.1.5)
29
+ i18n (0.7.0)
30
+ json (1.8.3)
31
+ minitest (4.7.5)
32
+ multi_json (1.11.2)
33
+ rainbow (2.0.0)
34
+ rake (10.4.2)
35
+ rspec (3.4.0)
36
+ rspec-core (~> 3.4.0)
37
+ rspec-expectations (~> 3.4.0)
38
+ rspec-mocks (~> 3.4.0)
39
+ rspec-core (3.4.1)
40
+ rspec-support (~> 3.4.0)
41
+ rspec-expectations (3.4.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.4.0)
44
+ rspec-mocks (3.4.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.4.0)
47
+ rspec-support (3.4.1)
48
+ simplecov (0.11.1)
49
+ docile (~> 1.1.0)
50
+ json (~> 1.8)
51
+ simplecov-html (~> 0.10.0)
52
+ simplecov-html (0.10.0)
53
+ sqlite3 (1.3.11)
54
+ thread_safe (0.3.5)
55
+ tzinfo (0.3.46)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ active_record_tasks (~> 1.1.0)
62
+ activerecord (~> 4.0.0)
63
+ activesupport (~> 4.0)
64
+ codeclimate-test-reporter
65
+ rake
66
+ rspec
67
+ sqlite3
68
+
69
+ BUNDLED WITH
70
+ 1.11.2
data/LICENSE.txt ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Marek Fajkus
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # ActiveRecordFlorder
2
+
3
+ [![Build Status](https://travis-ci.org/turboMaCk/active-record-florder.svg)](https://travis-ci.org/turboMaCk/active-record-florder)
4
+ [![Code Climate](https://codeclimate.com/github/turboMaCk/active-record-florder/badges/gpa.svg)](https://codeclimate.com/github/turboMaCk/active-record-florder)
5
+ [![Test Coverage](https://codeclimate.com/github/turboMaCk/active-record-florder/badges/coverage.svg)](https://codeclimate.com/github/turboMaCk/active-record-florder/coverage)
6
+
7
+ Floating point ActiveRecord models ordering for rich client apps heavily inspirated by Trello's ordering alorithm.
8
+ ActiveRecordFlorder let client decide position of model in collection normalize given value and resolve conflicting positions in collection
9
+ to keep your data clean. It's highly optimalized and generate as small SQL queries as possible.
10
+ The whole philosophy is to load and update as little records as possible so in 99% it runs just one SELECT and one UPDATE.
11
+ In edge cases sanitization of all records happens and bring records back to the Garden of Eden.
12
+ It's implemented with both Rails and non-Rails apps in mind and highly configurable.
13
+
14
+ THIS SOFTWARE IS STILL IN BETA!
15
+
16
+ ## Installation
17
+
18
+ add to your Gemfile if you're using Bundler
19
+
20
+ ```ruby
21
+ gem 'active_record_florder', git: 'https://github.com/turboMaCk/active-record-florder.git'
22
+ ```
23
+
24
+ Ruby gem comming soon!
25
+
26
+ ## Api
27
+
28
+ This gem defines new method for ActiveRecord::Base named `florder`.
29
+
30
+ ### Parameters
31
+
32
+ * direction {Symbol} values: `:asc` `:desc`, **required**
33
+ * options {Hash}, optional
34
+
35
+ ### Options
36
+
37
+ * scope {Symbol} - ordering scope (should be relationship or any other model property)
38
+ * attribute {Sumbol} - position column name, **default:** position
39
+ * min_delta {Number} - Minimal allowed position delata, affect position normalization *
40
+ * step: {Number} - Optimal (init) delta between positions *
41
+
42
+ * *Setting this should affect performance. We recommend using default values*
43
+
44
+ ## Usage
45
+
46
+ If you're using Rails or ActiveRecordMigrations create migration for your models:
47
+
48
+ ```ruby
49
+ class AddPositionToPosts < ActiveRecord::Migration
50
+ def change
51
+ add_column :posts, :position, default: 0
52
+ end
53
+ end
54
+ ```
55
+
56
+ With rails you can use generator for this:
57
+
58
+ ```shell
59
+ rails g migration add_position_to_posts position:float
60
+ ```
61
+
62
+ Now migrate your databse:
63
+
64
+ ```shell
65
+ rake db:migrate
66
+ ```
67
+
68
+ To use ActiveRecordFlorder add this line to your model:
69
+
70
+ ```ruby
71
+ class Post < ActiveRecord::Base
72
+ florder :desc
73
+ end
74
+ ```
75
+
76
+ Now you can start using it:
77
+
78
+ ```ruby
79
+ # Get ordered Posts
80
+ Post.ordered
81
+
82
+ # change position of first post
83
+ Post.first.move(123.123)
84
+ ```
85
+
86
+ ### Migrating Existing Data
87
+
88
+ ActiveRecordFlorder adds new before save model hook create position for new records. To make this work correctly you will need to initialize
89
+ positions for your existing data. ActiveRecordFlorder provides simple class method for this. You can define rake task or call it right inside your migration.
90
+
91
+ ```ruby
92
+ Posts.reinit_positions
93
+ ```
94
+
95
+ This method also should be used for optimalizing or repairing positions. It will not affect order of records just generate optimal positions for current order.
96
+
97
+ ### ASCending vs DESCending ordering
98
+
99
+ The only required parameter for each model is `:asc` or `:desc` order param.
100
+ Each of them is optimalised for one of the cases - creating new models as first or last.
101
+ Basically created model has highest position value in collection. Because of this whole interval is increased with each new record.
102
+ Negative values are not allowed so creating new models with lowest position will decrese number of possible positions and increase possibility in conflicts.
103
+ This is why this is desibled by default.
104
+
105
+ #### Simply use one of this:
106
+
107
+ * **Use ASC ordering when you want to create new models as last**
108
+ * **Use DESC ordering when you want to create new models as first**
109
+
110
+ ### Front-End Implementation
111
+
112
+ Client itself request position for given Model. This Gem is build with drag and drop interfaces in first place in mind.
113
+ Calculation on is simple - you just need to now position of two sibling to place model in middle of them.
114
+ For first and last position simply use hiher/lower value than first / last.
115
+
116
+ Here is teoretical implementation in javascript:
117
+
118
+ ```js
119
+ /*
120
+ * Calculate new requested position for model
121
+ * @param abovePosition {number}
122
+ * @param belowPosition {number}
123
+ * @returns {number}
124
+ */
125
+ function calculateNewPosition(abovePosition, belowPosition) {
126
+ if (!abovePosition) {
127
+ return bellowPosition ? bellowPosition * 2 : false;
128
+ } else if (!bellowPosition) {
129
+ return abovePosition / 2;
130
+ }
131
+
132
+ return (bellowPosition + abovePosition) / 2;
133
+ }
134
+ ```
135
+
136
+ ## Configuration
137
+
138
+ You can use initializer to overwrite default settings like this:
139
+
140
+ ```Ruby
141
+ ActiveRecordFlorder.configure do |config|
142
+ config.scope :owner
143
+ config.attribute :position_2
144
+ config.step 3
145
+ config.min_delta 0.1
146
+ end
147
+ ```
148
+
149
+ ## Developing
150
+
151
+ Git clone repository and cd in it:
152
+
153
+ ```shell
154
+ git clone git@github.com:turboMaCk/active-record-florder.git
155
+ cd active-record-florder
156
+ ```
157
+
158
+ Copy configuration:
159
+
160
+ ```shell
161
+ cp db/config.example.yml db/config.yml
162
+ ```
163
+
164
+ Install dependencies:
165
+
166
+ ```shell
167
+ bundle install
168
+ ```
169
+
170
+ Run tests:
171
+
172
+ ```shell
173
+ bundle exec rspec
174
+ ```
175
+
176
+ or
177
+
178
+ ```shell
179
+ bundle exec rake spec
180
+ ```
181
+
182
+ That's it! We can't wait for your PR!
183
+
184
+ ## License
185
+
186
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "active_record_tasks"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ ActiveRecordTasks.configure do |config|
6
+ # These are all the default values
7
+ config.db_dir = 'db'
8
+ config.db_config_path = 'db/config.yml'
9
+ config.env = 'test'
10
+ end
11
+
12
+ begin
13
+ # Run this AFTER you've configured
14
+ ActiveRecordTasks.load_tasks
15
+
16
+ require 'rspec/core/rake_task'
17
+
18
+ RSpec::Core::RakeTask.new(:spec)
19
+
20
+ task :default => :spec
21
+ rescue LoadError
22
+ # no rspec available
23
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record_florder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record_florder"
8
+ spec.version = ActiveRecordFlorder::VERSION
9
+ spec.authors = ["Marek Fajkus"]
10
+ spec.email = ["marek.faj@gmail.com"]
11
+
12
+ spec.summary = "Floating point ActiveRecord ordering for rich client apps"
13
+ spec.description = "Floating point ActiveRecord models ordering for rich client apps heavily inspirated by Trello's ordering alorithm. ActiveRecordFlorder let client decide position of model in collection normalize given value and resolve conflicting positions in collection to keep your data clean. It's highly optimalized and generate as small SQL queries as possible. The whole philosophy is to load and update as little records as possible so in 99% it runs just one SELECT and one UPDATE. In edge cases sanitization of all records happens and bring records back to the Garden of Eden. It's implemented with both Rails and non-Rails apps in mind and highly configurable."
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
data/bin/bundler ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'bundler' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("bundler", "bundler")
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'cc-tddium-post-worker' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("codeclimate-test-reporter", "cc-tddium-post-worker")
data/bin/htmldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'htmldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("diff-lcs", "htmldiff")
data/bin/ldiff ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'ldiff' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("diff-lcs", "ldiff")
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require "pathname"
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require "rubygems"
14
+ require "bundler/setup"
15
+
16
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,5 @@
1
+ {
2
+ "result": {
3
+ "covered_percent": 95.86
4
+ }
5
+ }