Skip to content

Commit 9540237

Browse files
committed
Added rails plugin code
1 parent 50a12f9 commit 9540237

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+964
-0
lines changed

Diff for: Gemfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
source "https://rubygems.org"
2+
3+
# Declare your gem's dependencies in singleton_rails.gemspec.
4+
# Bundler will treat runtime dependencies like base dependencies, and
5+
# development dependencies will be added by default to the :development group.
6+
gemspec
7+
8+
# Declare any dependencies that are still in development here instead of in
9+
# your gemspec. These might include edge Rails or gems from your path or
10+
# Git. Remember to move these dependencies to your gemspec before releasing
11+
# your gem to rubygems.org.
12+
13+
# To use debugger
14+
# gem 'debugger'

Diff for: Gemfile.lock

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
PATH
2+
remote: .
3+
specs:
4+
singleton-rails (0.0.1)
5+
rails (~> 4.1.0.beta1)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
actionmailer (4.1.0.beta1)
11+
actionpack (= 4.1.0.beta1)
12+
actionview (= 4.1.0.beta1)
13+
mail (~> 2.5.4)
14+
actionpack (4.1.0.beta1)
15+
actionview (= 4.1.0.beta1)
16+
activesupport (= 4.1.0.beta1)
17+
rack (~> 1.5.2)
18+
rack-test (~> 0.6.2)
19+
actionview (4.1.0.beta1)
20+
activesupport (= 4.1.0.beta1)
21+
builder (~> 3.1)
22+
erubis (~> 2.7.0)
23+
activemodel (4.1.0.beta1)
24+
activesupport (= 4.1.0.beta1)
25+
builder (~> 3.1)
26+
activerecord (4.1.0.beta1)
27+
activemodel (= 4.1.0.beta1)
28+
activesupport (= 4.1.0.beta1)
29+
arel (~> 5.0.0)
30+
activesupport (4.1.0.beta1)
31+
i18n (~> 0.6, >= 0.6.9)
32+
json (~> 1.7, >= 1.7.7)
33+
minitest (~> 5.1)
34+
thread_safe (~> 0.1)
35+
tzinfo (~> 1.1)
36+
arel (5.0.0)
37+
atomic (1.1.14)
38+
builder (3.2.2)
39+
erubis (2.7.0)
40+
hike (1.2.3)
41+
i18n (0.6.9)
42+
json (1.8.1)
43+
mail (2.5.4)
44+
mime-types (~> 1.16)
45+
treetop (~> 1.4.8)
46+
mime-types (1.25.1)
47+
minitest (5.2.2)
48+
multi_json (1.8.4)
49+
polyglot (0.3.3)
50+
rack (1.5.2)
51+
rack-test (0.6.2)
52+
rack (>= 1.0)
53+
rails (4.1.0.beta1)
54+
actionmailer (= 4.1.0.beta1)
55+
actionpack (= 4.1.0.beta1)
56+
actionview (= 4.1.0.beta1)
57+
activemodel (= 4.1.0.beta1)
58+
activerecord (= 4.1.0.beta1)
59+
activesupport (= 4.1.0.beta1)
60+
bundler (>= 1.3.0, < 2.0)
61+
railties (= 4.1.0.beta1)
62+
sprockets-rails (~> 2.0.0)
63+
railties (4.1.0.beta1)
64+
actionpack (= 4.1.0.beta1)
65+
activesupport (= 4.1.0.beta1)
66+
rake (>= 0.8.7)
67+
thor (>= 0.18.1, < 2.0)
68+
rake (10.1.1)
69+
sprockets (2.10.1)
70+
hike (~> 1.2)
71+
multi_json (~> 1.0)
72+
rack (~> 1.0)
73+
tilt (~> 1.1, != 1.3.0)
74+
sprockets-rails (2.0.1)
75+
actionpack (>= 3.0)
76+
activesupport (>= 3.0)
77+
sprockets (~> 2.8)
78+
sqlite3 (1.3.8)
79+
thor (0.18.1)
80+
thread_safe (0.1.3)
81+
atomic
82+
tilt (1.4.1)
83+
treetop (1.4.15)
84+
polyglot
85+
polyglot (>= 0.3.1)
86+
tzinfo (1.1.0)
87+
thread_safe (~> 0.1)
88+
89+
PLATFORMS
90+
ruby
91+
92+
DEPENDENCIES
93+
singleton-rails!
94+
sqlite3

Diff for: MIT-LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright 2014 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.

Diff for: README.rdoc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
= SingletonRails
2+
3+
This project rocks and uses MIT-LICENSE.

Diff for: Rakefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
begin
2+
require 'bundler/setup'
3+
rescue LoadError
4+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5+
end
6+
7+
require 'rdoc/task'
8+
9+
RDoc::Task.new(:rdoc) do |rdoc|
10+
rdoc.rdoc_dir = 'rdoc'
11+
rdoc.title = 'SingletonRails'
12+
rdoc.options << '--line-numbers'
13+
rdoc.rdoc_files.include('README.rdoc')
14+
rdoc.rdoc_files.include('lib/**/*.rb')
15+
end
16+
17+
18+
19+
20+
Bundler::GemHelper.install_tasks
21+
22+
require 'rake/testtask'
23+
24+
Rake::TestTask.new(:test) do |t|
25+
t.libs << 'lib'
26+
t.libs << 'test'
27+
t.pattern = 'test/**/*_test.rb'
28+
t.verbose = false
29+
end
30+
31+
32+
task default: :test

Diff for: lib/active_record/singleton.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module ActiveRecord
2+
module Singleton
3+
extend ActiveSupport::Concern
4+
5+
module ClassMethods
6+
7+
def instance
8+
first || new
9+
end
10+
11+
end
12+
13+
private
14+
15+
def create_or_update
16+
run_callbacks(:save) do
17+
raise ReadOnlyRecord if readonly?
18+
19+
# Singleton record
20+
singleton = self.class.first
21+
22+
# Force overriding the one-and-only singleton record (if any)
23+
self.id = singleton.id if singleton
24+
25+
# Update if a singleton record is found, otherwise insert one
26+
result = singleton ? update_record : create_record
27+
result != false
28+
end
29+
end
30+
31+
end
32+
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module RailsAdmin::Config::Actions::SingletonAwareDelete
2+
3+
PATCH = Proc.new do
4+
5+
# Hide this delete action when dealing with a singleton model
6+
visible do
7+
bindings[:abstract_model].model.included_modules.exclude? ActiveRecord::Singleton
8+
end
9+
10+
end
11+
12+
end
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module RailsAdmin::Config::Actions::SingletonAwareIndex
2+
3+
PATCH = Proc.new do
4+
5+
# Hide this index action when dealing with a singleton model
6+
visible do
7+
bindings[:abstract_model].model.included_modules.exclude? ActiveRecord::Singleton
8+
end
9+
10+
# Store reference to the original controller procedure
11+
original_controller = controller
12+
13+
register_instance_option :controller do
14+
abstract_model = bindings[:abstract_model]
15+
model = abstract_model.model
16+
17+
# Determine whether this is a singleton model
18+
if model.included_modules.include? ActiveRecord::Singleton
19+
Proc.new do
20+
21+
# Redirect to new/show view depending on whether a record already exists
22+
if singleton = model.first
23+
redirect_to show_path abstract_model.to_param, singleton.id
24+
else
25+
redirect_to new_path abstract_model.to_param
26+
end
27+
end
28+
else
29+
original_controller
30+
end
31+
end
32+
33+
end
34+
35+
end

Diff for: lib/singleton-rails.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module SingletonRails
2+
end

Diff for: lib/tasks/singleton_rails_tasks.rake

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# desc "Explaining what the task does"
2+
# task :singleton_rails do
3+
# # Task goes here
4+
# end

Diff for: singleton_rails.gemspec

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$:.push File.expand_path("../lib", __FILE__)
2+
3+
# Describe your gem and declare its dependencies:
4+
Gem::Specification.new do |s|
5+
s.name = "singleton-rails"
6+
s.version = "0.0.1"
7+
s.authors = ["Tim Kurvers", "Christoffer Winterkvist"]
8+
9+
s.homepage = "http://hyper.no"
10+
s.summary = "Forever Alone Ruby Gem"
11+
s.description = "Adds singleton functionallity to ActiveRecord models"
12+
s.license = "MIT"
13+
14+
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
15+
s.test_files = Dir["test/**/*"]
16+
17+
s.add_dependency "rails", "~> 4.1.0.beta1"
18+
19+
s.add_development_dependency "sqlite3"
20+
end

Diff for: test/dummy/README.rdoc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
== README
2+
3+
This README would normally document whatever steps are necessary to get the
4+
application up and running.
5+
6+
Things you may want to cover:
7+
8+
* Ruby version
9+
10+
* System dependencies
11+
12+
* Configuration
13+
14+
* Database creation
15+
16+
* Database initialization
17+
18+
* How to run the test suite
19+
20+
* Services (job queues, cache servers, search engines, etc.)
21+
22+
* Deployment instructions
23+
24+
* ...
25+
26+
27+
Please feel free to use a different markup language if you do not plan to run
28+
<tt>rake doc:app</tt>.

Diff for: test/dummy/Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require File.expand_path('../config/application', __FILE__)
5+
6+
Rails.application.load_tasks

Diff for: test/dummy/app/assets/images/.keep

Whitespace-only changes.

Diff for: test/dummy/app/assets/javascripts/application.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This is a manifest file that'll be compiled into application.js, which will include all the files
2+
// listed below.
3+
//
4+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6+
//
7+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8+
// compiled file.
9+
//
10+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11+
// about supported directives.
12+
//
13+
//= require_tree .

Diff for: test/dummy/app/assets/stylesheets/application.css

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* This is a manifest file that'll be compiled into application.css, which will include all the files
3+
* listed below.
4+
*
5+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7+
*
8+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
9+
* compiled file so the styles you add here take precedence over styles defined in any styles
10+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11+
* file per style scope.
12+
*
13+
*= require_tree .
14+
*= require_self
15+
*/

Diff for: test/dummy/app/controllers/application_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ApplicationController < ActionController::Base
2+
# Prevent CSRF attacks by raising an exception.
3+
# For APIs, you may want to use :null_session instead.
4+
protect_from_forgery with: :exception
5+
end

Diff for: test/dummy/app/controllers/concerns/.keep

Whitespace-only changes.

Diff for: test/dummy/app/helpers/application_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ApplicationHelper
2+
end

Diff for: test/dummy/app/mailers/.keep

Whitespace-only changes.

Diff for: test/dummy/app/models/.keep

Whitespace-only changes.

Diff for: test/dummy/app/models/concerns/.keep

Whitespace-only changes.

Diff for: test/dummy/app/views/layouts/application.html.erb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Dummy</title>
5+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7+
<%= csrf_meta_tags %>
8+
</head>
9+
<body>
10+
11+
<%= yield %>
12+
13+
</body>
14+
</html>

Diff for: test/dummy/bin/bundle

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env ruby
2+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3+
load Gem.bin_path('bundler', 'bundle')

Diff for: test/dummy/bin/rails

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
APP_PATH = File.expand_path('../../config/application', __FILE__)
3+
require_relative '../config/boot'
4+
require 'rails/commands'

Diff for: test/dummy/bin/rake

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env ruby
2+
require_relative '../config/boot'
3+
require 'rake'
4+
Rake.application.run

Diff for: test/dummy/config.ru

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file is used by Rack-based servers to start the application.
2+
3+
require ::File.expand_path('../config/environment', __FILE__)
4+
run Rails.application

0 commit comments

Comments
 (0)