activerecord-positionable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a2f14396f9d8b36e0353c25ad63a8ca93190836
4
+ data.tar.gz: 840cbe9aec1c8ce3e37bee1be5c75f5c7c26909d
5
+ SHA512:
6
+ metadata.gz: c6ed77844e86733e579733be5041b19da66fee52a981e5796334eb34d2512436fbe6f5b9b9c9847d968906d7b654b6de1b95b62c47e5de049d5782080244c31a
7
+ data.tar.gz: a6b51399a16b340b30385ecd6eaf187e7b30fa1e2a257e568e32c461072f2185207f62cd2b70bd9cb2781bdeeefd31a60a3190757f574da5b181e3c1ee3d0571
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in confo.gemspec
4
+ gemspec
5
+
6
+ gem 'essay', path: '~/Development/essay'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yaroslav Konoplov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ ```ruby
2
+ class Article < ActiveRecord::Base
3
+ positionable
4
+ end
5
+
6
+ Article.features.positionable? # => true
7
+ Article.attribute_roles[:position].position? # => true
8
+ ```
9
+
10
+ ## Gemfile
11
+ ```ruby
12
+ gem 'activerecord-positionable', github: 'yivo/activerecord-positionable'
13
+ ```
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'activerecord-positionable'
5
+ s.version = '1.0.0'
6
+ s.authors = ['Yaroslav Konoplov']
7
+ s.email = ['yaroslav@inbox.com']
8
+ s.summary = 'ActiveRecord positionable extension'
9
+ s.description = 'ActiveRecord positionable extension'
10
+ s.homepage = 'http://github.com/yivo/activerecord-positionable'
11
+ s.license = 'MIT'
12
+
13
+ s.executables = `git ls-files -z -- bin/*`.split("\x0").map{ |f| File.basename(f) }
14
+ s.files = `git ls-files -z`.split("\x0")
15
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
16
+ s.require_paths = ['lib']
17
+
18
+ s.add_dependency 'activesupport', '>= 3.2.0'
19
+ s.add_dependency 'activerecord', '>= 3.0'
20
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/all'
2
+ require 'active_record'
3
+
4
+ require 'activerecord-positionable/extension'
5
+ require 'activerecord-positionable/migration'
6
+ require 'activerecord-positionable/essay' if defined?(Essay)
7
+
8
+ ActiveRecord::Base.include(Positionable::Extension)
@@ -0,0 +1,21 @@
1
+ module Essay
2
+ class ModelFeatures
3
+ def positionable?
4
+ model_class.positionable_options.any?
5
+ end
6
+
7
+ serialize do
8
+ { is_positionable: positionable? }
9
+ end
10
+ end
11
+
12
+ class AttributeFeatures
13
+ def position?
14
+ model_class.attributes_marked_as_position.include?(attribute_name)
15
+ end
16
+
17
+ serialize do
18
+ { is_position: position? }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ module Positionable
2
+ module Extension
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # Stores scope name, position attribute name
7
+ class_attribute :positionable_options, instance_accessor: false, instance_predicate: false
8
+
9
+ class_attribute :attributes_marked_as_position, instance_accessor: false, instance_predicate: false
10
+
11
+ self.positionable_options = {}
12
+ self.attributes_marked_as_position = []
13
+ end
14
+
15
+ module ClassMethods
16
+ def positionable(attr_name = :position)
17
+ position_attr(attr_name)
18
+
19
+ scope :ordered, -> { order(Hash[attr_name, :desc]).order(id: :desc) }
20
+ scope :latest, -> { ordered }
21
+
22
+ self.positionable_options = positionable_options.deep_dup.merge!(
23
+ attribute: attr_name,
24
+ scope: :ordered
25
+ )
26
+ end
27
+
28
+ def position_attr(*attr_names)
29
+ attr_names.each do |attr_name|
30
+ after_save :"assign_#{attr_name}!", if: :"#{attr_name}_missing?"
31
+
32
+ class_eval <<-BODY, __FILE__, __LINE__ + 1
33
+ def #{attr_name}_missing?
34
+ self[:#{attr_name}].blank?
35
+ end
36
+
37
+ def assign_#{attr_name}!
38
+ self[:#{attr_name}] = id
39
+ save!
40
+ end
41
+ BODY
42
+ end
43
+ self.attributes_marked_as_position += attr_names
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ class ActiveRecord::ConnectionAdapters::TableDefinition
2
+ def position(*args)
3
+ options = { null: false, index: true, default: 0 }.merge!(args.extract_options!)
4
+ column_names = args.presence || [:position]
5
+ column_names.each { |name| column(name, :integer, options) }
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-positionable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Konoplov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: ActiveRecord positionable extension
42
+ email:
43
+ - yaroslav@inbox.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - activerecord-positionable.gemspec
54
+ - lib/activerecord-positionable.rb
55
+ - lib/activerecord-positionable/essay.rb
56
+ - lib/activerecord-positionable/extension.rb
57
+ - lib/activerecord-positionable/migration.rb
58
+ homepage: http://github.com/yivo/activerecord-positionable
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: ActiveRecord positionable extension
82
+ test_files: []