primary 0.0.1

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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ #Primary
2
+
3
+ `primary` is simple gem/plugin that will help you ensure, that you have only one primary / default record in DB. Ie. your site can have multiple languages (stored in db), but only one of them is primary / default.
4
+
5
+ ## Instalation
6
+
7
+ Simply add `gem 'primary'` to your `Gemfile`.
8
+
9
+ After running `bundle install` you can mark model as acting as primary / default.
10
+
11
+ ## Usage
12
+
13
+ To your model (ie. `Language`) add `is_primary` column and declare models as beeing primary by adding `is_primary` declaration:
14
+ ```ruby
15
+ class Language < ActiveRecord::Base
16
+ is_primary
17
+
18
+ attr_accessible :language, :is_primary
19
+ end
20
+ ```
21
+ ###Sample
22
+ ```ruby
23
+ Language.count
24
+ # => 0
25
+
26
+ lang_en = Language.create({:language=>'en'})
27
+ lang_en.is_primary
28
+ # => true
29
+
30
+ lang_de = Language.create({:language=>'de'})
31
+ lang_de.is.primary
32
+ # => false
33
+
34
+ lang_de.is_primary = true
35
+ lang_de.save
36
+ lang_de.is_primary
37
+ # => true
38
+
39
+ lang_en.reload
40
+ lang_en.is_primary
41
+ # => false
42
+ ```
43
+ ###Possible options
44
+ ```ruby
45
+ is_primary :on=>'is_default', :scope=>'site_id'
46
+
47
+ :on => 'is_default' # use different column name
48
+ :scope => 'site_id' # scope default to different column - ie. you'll
49
+ # be able to define multiple languages per multiple
50
+ # sites and each site will have one default lang.
51
+ ```
52
+
53
+ ##Requirements
54
+
55
+ Currently gem requires Rails 3.2, I'll have to check, if it works as desired with lower versions of Ruby.
56
+
57
+ ##License
58
+
59
+ Project is distributed under general terms of MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Primary'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+ require 'rspec/core/rake_task'
25
+
26
+ desc 'Default: Run all specs for a specific rails version.'
27
+ task :default => :spec
28
+
29
+ desc "Run all specs for a specific rails version"
30
+ RSpec::Core::RakeTask.new(:spec) do |t|
31
+ t.pattern = '**/*_spec.rb'
32
+ end
33
+
34
+
35
+
36
+ Bundler::GemHelper.install_tasks
37
+
@@ -0,0 +1,3 @@
1
+ module Primary
2
+ VERSION = "0.0.1"
3
+ end
data/lib/primary.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Primary
2
+ def is_primary options={}
3
+ cattr_accessor :primary_is_primary_opts
4
+ self.primary_is_primary_opts = options.reverse_merge({
5
+ :on => :is_primary,
6
+ :scope => nil
7
+ })
8
+
9
+ before_save :primary_is_primary_mark
10
+ after_save :primary_is_primary_take_care
11
+ include InstanceMethods
12
+ end
13
+ module InstanceMethods
14
+ private
15
+ def primary_is_primary_mark
16
+ options = self.class.primary_is_primary_opts
17
+ check = get_primary_scope(options)
18
+ if check.count == 0
19
+ self.send("#{options[:on]}=", true)
20
+ end
21
+ end
22
+ def primary_is_primary_take_care
23
+ options = self.class.primary_is_primary_opts
24
+ if self.send("#{options[:on]}?")==true
25
+ scope = get_primary_scope(options)
26
+ scope = scope.where('id!=?', self.id)
27
+ scope.update_all(["#{options[:on]}=?", false])
28
+ end
29
+ end
30
+
31
+ def get_primary_scope(options)
32
+ check = self.class.where("#{options[:on].to_s} = ?", true)
33
+ if options[:scope]
34
+ check = check.where("#{options[:scope].to_s} = ?", self.send(options[:scope]))
35
+ end
36
+ check
37
+ end
38
+ end
39
+ end
40
+ ActiveRecord::Base.extend Primary
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :primary do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: primary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maciej Litwiniuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-21 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70330926958120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70330926958120
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70330926957700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70330926957700
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-rails
38
+ requirement: &70330926957240 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70330926957240
47
+ - !ruby/object:Gem::Dependency
48
+ name: factory_girl
49
+ requirement: &70330926956820 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70330926956820
58
+ description: This simple gem will help you with keeping default or primary record
59
+ (one record that is marked as primary) - ie. default domain or language for site
60
+ email:
61
+ - maciej@litwiniuk.net
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/primary/version.rb
67
+ - lib/primary.rb
68
+ - lib/tasks/primary_tasks.rake
69
+ - MIT-LICENSE
70
+ - Rakefile
71
+ - README.md
72
+ homepage: https://github.com/prograils/primary
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.10
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Simple gem that will help with primary/default records
96
+ test_files: []
97
+ has_rdoc: