simple_acts_as_list 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ .rspec
20
+ bin/
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_acts_as_list.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 Redu Educational Technologies
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.
23
+
@@ -0,0 +1,57 @@
1
+ # SimpleActsAsList [![Build Status](https://travis-ci.org/redu/simple_acts_as_list.png?branch=master)](https://travis-ci.org/redu/simple_acts_as_list) [![Dependency Status](https://gemnasium.com/redu/simple_acts_as_list.png)](https://gemnasium.com/redu/simple_acts_as_list)
2
+
3
+ Provides very simple list behavior.
4
+ Based on [acts_as_list gem](https://github.com/swanandp/acts_as_list).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'simple_acts_as_list'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install simple_acts_as_list
19
+
20
+ ## Usage
21
+
22
+ You just need to include the model and specify the scope. Of course, the object need to have :
23
+ 1. An accessor for the collection, for example `items`.
24
+ 2. And column responsible to order the items in the list (`position`).
25
+
26
+ ```ruby
27
+ class TodoList ; end
28
+
29
+ class Item
30
+ include ActsAsList
31
+ simple_acts_as_list :scope => :todo_list_id
32
+ end
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
42
+
43
+
44
+ <img src="https://github.com/downloads/redu/redupy/redutech-marca.png" alt="Redu Educational Technologies" width="300">
45
+
46
+ This project is maintained and funded by [Redu Educational Techologies](http://tech.redu.com.br).
47
+
48
+ # Copyright
49
+
50
+ Copyright (c) 2013 Redu Educational Technologies
51
+
52
+ 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:
53
+
54
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
55
+
56
+ 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.
57
+
@@ -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
@@ -0,0 +1,6 @@
1
+ require "simple_acts_as_list/version"
2
+ require "simple_acts_as_list/model_additions"
3
+
4
+ module SimpleActsAsList
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,102 @@
1
+ require 'active_support'
2
+
3
+ module SimpleActsAsList::ModelAdditions
4
+ # Provides very simple list's behavior.
5
+ #
6
+ # A minimal implementation could be:
7
+ #
8
+ # class Item
9
+ # include SimpleActsAsList::ModelAdditions
10
+ # simple_acts_as_list :scope => :todo_list_id
11
+ # end
12
+
13
+ extend ActiveSupport::Concern
14
+
15
+ included do
16
+ before_create :update_position
17
+ end
18
+
19
+ module ClassMethods
20
+ # Adds methods to consult informations about the list where the element belongs
21
+ #
22
+ # :scope => Attribute that defines the list scope
23
+ def simple_acts_as_list(opts={})
24
+ cattr_accessor :simple_acts_as_list_scope_id
25
+ self.simple_acts_as_list_scope_id = opts[:scope]
26
+ end
27
+
28
+ # Scope that returns item's list ordered by position
29
+ def simple_acts_as_list_scope(scope_id)
30
+ self.where(
31
+ "#{self.table_name}.#{self.simple_acts_as_list_scope_id}" => scope_id).
32
+ order("#{self.table_name}.position")
33
+ end
34
+ end
35
+
36
+ # Returns +true+ if the item is the last one of the list
37
+ def last_item?
38
+ self.simple_acts_as_list_scope.last == self
39
+ end
40
+
41
+ # Returns +true+ if the item is the first one of the list
42
+ def first_item?
43
+ self.simple_acts_as_list_scope.first == self
44
+ end
45
+
46
+ # Returns last list item
47
+ def last_item
48
+ self.simple_acts_as_list_scope.last
49
+ end
50
+
51
+ # Returns first list item
52
+ def first_item
53
+ self.simple_acts_as_list_scope.first
54
+ end
55
+
56
+ # Returns next list item
57
+ #
58
+ # Returns +nil+ if the item is the last one of the list
59
+ def next_item
60
+ index = self.simple_acts_as_list_scope.index(self)
61
+ self.simple_acts_as_list_scope[index + 1]
62
+ end
63
+
64
+ # Returns previous list item
65
+ #
66
+ # Returns +nil+ if the item is the first one of the list
67
+ def previous_item
68
+ index = self.simple_acts_as_list_scope.index(self)
69
+ index == 0 ? nil : self.simple_acts_as_list_scope[index - 1]
70
+ end
71
+
72
+ # Returns the item with +offset+ position based on current item
73
+ #
74
+ # Example:
75
+ # @todo = Todo.create
76
+ # @todo_2 = Todo.create
77
+ # @todo.item_at_offset(1) # returns @todo_2
78
+ # @todo_2.item_at_offset(-1) # returns @todo
79
+ #
80
+ def item_at_offset(offset)
81
+ item_index = self.simple_acts_as_list_scope.index(self)
82
+ index = item_index + offset
83
+ index < 0 ? nil : self.simple_acts_as_list_scope[index]
84
+ end
85
+
86
+ protected
87
+
88
+ # Updates item's position that is being created
89
+ def update_position
90
+ if self.simple_acts_as_list_scope.length == 0
91
+ self.position = 1
92
+ else
93
+ self.position = self.simple_acts_as_list_scope.last.position + 1
94
+ end
95
+ end
96
+
97
+ # Auxiliar method to invoke the scope that returns the list
98
+ def simple_acts_as_list_scope
99
+ scope_id = self.send(self.class.simple_acts_as_list_scope_id)
100
+ self.class.simple_acts_as_list_scope(scope_id)
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleActsAsList
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_acts_as_list/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple_acts_as_list"
8
+ gem.version = SimpleActsAsList::VERSION
9
+ gem.authors = ["Juliana Lucena"]
10
+ gem.email = ["julianalucenaa@gmail.com"]
11
+ gem.description = %q{Provides a very simple list behavior based on a scope.}
12
+ gem.summary = %q{Provides a very simple list behavior based on a scope.}
13
+ gem.homepage = "https://github.com/redu/simple_acts_as_list"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^spec/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'activesupport'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'sqlite3'
25
+ gem.add_development_dependency 'activerecord'
26
+ end
@@ -0,0 +1,162 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleActsAsList::ModelAdditions do
4
+ context ".create" do
5
+ context "without position" do
6
+ context "and the list is empty" do
7
+ let(:todo_list) { create_todo_list(:qtt_items => 0) }
8
+ let(:item) do
9
+ Item.create(:name => "Item", :todo_list => todo_list)
10
+ end
11
+
12
+ it "should set the position to 1" do
13
+ item.reload
14
+ item.position.should_not be_nil
15
+ item.position.should == 1
16
+ end
17
+ end
18
+
19
+ context "and the list is not empty" do
20
+ let(:todo_list) do
21
+ create_todo_list(:qtt_items => 3)
22
+ end
23
+ let(:item) do
24
+ Item.create(:name => "Item", :todo_list => todo_list)
25
+ end
26
+
27
+ it "should set the position to 4" do
28
+ item.reload
29
+ item.position.should_not be_nil
30
+ item.position.should == 4
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+ context "list methods" do
38
+ let(:todo_list) { create_todo_list(:qtt_items => 3) }
39
+
40
+ context "responders" do
41
+ let(:item) { Item.new(:name => "Item") }
42
+
43
+ [:position, :last_item?, :first_item?, :last_item, :first_item,
44
+ :next_item, :previous_item].each do |method|
45
+ it "should respond to #{method}" do
46
+ item.should respond_to(method)
47
+ end
48
+ end
49
+ end
50
+
51
+ context "#last_item?" do
52
+ it "should return true when the item is the last" do
53
+ last = todo_list.items.last
54
+ last.should be_last_item
55
+ end
56
+
57
+ it "should return false when the item is not the last" do
58
+ first = todo_list.items.first
59
+ first.should_not be_last_item
60
+
61
+ middle = todo_list.items[1]
62
+ middle.should_not be_last_item
63
+ end
64
+ end
65
+
66
+ context "#first_item?" do
67
+ it "should return true when the item is the first" do
68
+ first = todo_list.items.first
69
+ first.should be_first_item
70
+ end
71
+
72
+ it "should return false when the item is not the first" do
73
+ last = todo_list.items.last
74
+ last.should_not be_first_item
75
+
76
+ middle = todo_list.items[1]
77
+ middle.should_not be_first_item
78
+ end
79
+ end
80
+
81
+ context "#last_item" do
82
+ it "should return the last item of the list" do
83
+ item = todo_list.items.first
84
+ item.last_item.should == todo_list.items.last
85
+ end
86
+ end
87
+
88
+ context "#first_item" do
89
+ it "should return the first item of the list" do
90
+ item = todo_list.items.last
91
+ item.first_item.should == todo_list.items.first
92
+ end
93
+ end
94
+
95
+ context "#next_item" do
96
+ it "should return the next item of the list" do
97
+ first = todo_list.items.first
98
+ first.next_item.should == todo_list.items[1]
99
+
100
+ item = todo_list.items[1]
101
+ item.next_item.should == todo_list.items[2]
102
+ end
103
+
104
+ it "should return nil if it is the last item of the list" do
105
+ last = todo_list.items.last
106
+ last.next_item.should be_nil
107
+ end
108
+ end
109
+
110
+ context "#previous_item" do
111
+ it "should return the previous item of the list" do
112
+ last = todo_list.items.last
113
+ length = todo_list.items.length
114
+ last.previous_item.should == todo_list.items[length - 2]
115
+
116
+ item = todo_list.items[1]
117
+ item.previous_item.should == todo_list.items[0]
118
+ end
119
+
120
+ it "should return nil if it is the first item of the list" do
121
+ first = todo_list.items.first
122
+ first.previous_item.should be_nil
123
+ end
124
+ end
125
+
126
+ context "#item_at_offset" do
127
+ it "should return the item with specified negative offset" do
128
+ item = todo_list.items.last
129
+ length = todo_list.items.length
130
+ item.item_at_offset(-2).should ==
131
+ todo_list.items[length - 3]
132
+ end
133
+
134
+ it "should return the item with specified positive offset" do
135
+ item = todo_list.items.first
136
+ length = todo_list.items.length
137
+ item.item_at_offset(2).should ==
138
+ todo_list.items[2]
139
+ end
140
+
141
+ it "should return nil if can't find the item with neg specified offset" do
142
+ item = todo_list.items.first
143
+ item.item_at_offset(-1).should be_nil
144
+ end
145
+
146
+ it "should return nil if can't find the item with pos specified offset" do
147
+ item = todo_list.items.last
148
+ item.item_at_offset(2).should be_nil
149
+ end
150
+ end
151
+ end
152
+
153
+ private
154
+
155
+ def create_todo_list(opts={})
156
+ qtt_items = opts[:qtt_items] || 1
157
+
158
+ todo_list = TodoList.create(:name => "My Todo List")
159
+ (1..qtt_items).each {|i| todo_list.items << Item.new(:name => "Item #{i}") }
160
+ todo_list
161
+ end
162
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ #
8
+ require 'simple_acts_as_list'
9
+ require 'support/setup_ar_and_schema'
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ config.order = 'random'
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3',
4
+ :database => ':memory:')
5
+
6
+ ActiveRecord::Schema.define do
7
+ create_table 'todo_lists', :force => true do |t|
8
+ t.string 'name'
9
+ end
10
+
11
+ create_table 'items', :force => true do |t|
12
+ t.string 'name'
13
+ t.integer 'todo_list_id'
14
+ t.integer 'position'
15
+ end
16
+ end
17
+
18
+ class TodoList < ActiveRecord::Base
19
+ has_many :items
20
+ end
21
+
22
+ class Item < ActiveRecord::Base
23
+ include SimpleActsAsList::ModelAdditions
24
+ simple_acts_as_list :scope => :todo_list_id
25
+
26
+ belongs_to :todo_list
27
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_acts_as_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Juliana Lucena
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Provides a very simple list behavior based on a scope.
95
+ email:
96
+ - julianalucenaa@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .travis.yml
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/simple_acts_as_list.rb
108
+ - lib/simple_acts_as_list/model_additions.rb
109
+ - lib/simple_acts_as_list/version.rb
110
+ - simple_acts_as_list.gemspec
111
+ - spec/model_additions_spec.rb
112
+ - spec/spec_helper.rb
113
+ - spec/support/setup_ar_and_schema.rb
114
+ homepage: https://github.com/redu/simple_acts_as_list
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.24
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Provides a very simple list behavior based on a scope.
138
+ test_files:
139
+ - spec/model_additions_spec.rb
140
+ - spec/spec_helper.rb
141
+ - spec/support/setup_ar_and_schema.rb