Skip to content

Commit ca85268

Browse files
author
Daniel Collins
committed
Update with PRs pluginaweek#1, pluginaweek#2, and pluginaweek#12, plus rubocopisms
1 parent 949894e commit ca85268

26 files changed

+1038
-875
lines changed

.rubocop.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
inherit_from: .rubocop_todo.yml
2+
3+
# Offense count: 7
4+
Metrics/AbcSize:
5+
Max: 54
6+
7+
# Offense count: 1
8+
Metrics/CyclomaticComplexity:
9+
Max: 8
10+
11+
# Offense count: 134
12+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
13+
# URISchemes: http, https
14+
Metrics/LineLength:
15+
Max: 221
16+
17+
# Offense count: 7
18+
# Configuration parameters: CountComments.
19+
Metrics/MethodLength:
20+
Max: 48
21+
22+
# Offense count: 1
23+
# Configuration parameters: CountComments.
24+
Metrics/ModuleLength:
25+
Max: 143
26+
27+
# Offense count: 1
28+
Metrics/PerceivedComplexity:
29+
Max: 10

.rubocop_todo.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2017-07-13 19:06:38 +0000 using RuboCop version 0.48.1.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 2
10+
Lint/AmbiguousBlockAssociation:
11+
Exclude:
12+
- 'lib/preferences.rb'
13+
14+
# Offense count: 2
15+
Lint/DuplicateMethods:
16+
Exclude:
17+
- 'test/functional/preferences_test.rb'
18+
19+
# Offense count: 1
20+
Lint/HandleExceptions:
21+
Exclude:
22+
- 'Rakefile'
23+
24+
# Offense count: 2
25+
Lint/ShadowingOuterLocalVariable:
26+
Exclude:
27+
- 'lib/preferences.rb'
28+
29+
# Offense count: 39
30+
Lint/UselessAssignment:
31+
Exclude:
32+
- 'test/functional/preferences_test.rb'
33+
34+
# Offense count: 4
35+
Style/Documentation:
36+
Exclude:
37+
- 'spec/**/*'
38+
- 'test/**/*'
39+
- 'generators/preferences/templates/001_create_preferences.rb'
40+
- 'lib/generators/preferences_generator.rb'
41+
- 'lib/preferences.rb'
42+
43+
# Offense count: 1
44+
Style/DoubleNegation:
45+
Exclude:
46+
- 'lib/preferences/preference_definition.rb'
47+
48+
# Offense count: 1
49+
# Configuration parameters: EnforcedStyle, SupportedStyles.
50+
# SupportedStyles: format, sprintf, percent
51+
Style/FormatString:
52+
Exclude:
53+
- 'lib/generators/preferences_generator.rb'
54+
55+
# Offense count: 2
56+
# Configuration parameters: AllowedVariables.
57+
Style/GlobalVars:
58+
Exclude:
59+
- 'test/test_helper.rb'
60+
61+
# Offense count: 1
62+
# Configuration parameters: MinBodyLength.
63+
Style/GuardClause:
64+
Exclude:
65+
- 'lib/preferences.rb'
66+
67+
# Offense count: 1
68+
# Cop supports --auto-correct.
69+
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
70+
# SupportedStyles: predicate, comparison
71+
Style/NumericPredicate:
72+
Exclude:
73+
- 'spec/**/*'
74+
- 'lib/preferences.rb'

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
source "http://www.rubygems.org"
2-
1+
source 'http://www.rubygems.org'
2+
33
gemspec

README.rdoc

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,37 @@ Write method:
123123
user.write_preference(:hot_salsa, false) # => false
124124
user.write_preference(:language, "English") # => "English"
125125

126+
=== Accessible preferences
127+
128+
If you want a preference to be accessible via the +attributes+ method on the
129+
model, use the +accessible_preference+ method:
130+
131+
class User < ActiveRecord::Base
132+
accessible_preference :hot_salsa
133+
accessible_preference :two_percent_milk
134+
end
135+
136+
Now, you can easily update all the preferences at once from your controllers:
137+
138+
In the view:
139+
140+
- form_for @user do |f|
141+
= f.check_box :prefers_hot_salsa
142+
= f.check_box :prefers_two_percent_milk
143+
144+
In the controller:
145+
146+
UsersController < ApplicationController
147+
def update
148+
@user = User.find(params[:id])
149+
@user.attributes = params[:user]
150+
151+
flash.notice = 'Saved preferences' if @user.save
152+
153+
render 'edit'
154+
end
155+
end
156+
126157
=== Accessing all preferences
127158

128159
To get the collection of all custom, stored preferences for a particular record,
@@ -152,7 +183,7 @@ through an example:
152183

153184
user = User.find(:first)
154185
car = Car.find(:first)
155-
186+
156187
user.preferred_color = 'red', car
157188
# user.write_preference(:color, 'red', car) # The generic way
158189

@@ -169,13 +200,13 @@ In addition to grouping preferences for a particular record, you can also group
169200
preferences by name. For example,
170201

171202
user = User.find(:first)
172-
203+
173204
user.preferred_color = 'red', :automobiles
174205
user.preferred_color = 'tan', :clothing
175-
206+
176207
user.preferred_color(:automobiles) # => "red"
177208
user.preferred_color(:clothing) # => "tan"
178-
209+
179210
user.preferences(:automobiles) # => {"color"=>"red"}
180211

181212
=== Saving preferences

Rakefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ require 'rake/testtask'
44
require 'rake/rdoctask'
55

66
desc 'Default: run all tests.'
7-
task :default => :test
7+
task default: :test
88

9-
desc "Test preferences."
9+
desc 'Test preferences.'
1010
Rake::TestTask.new(:test) do |t|
1111
t.libs << 'lib'
1212
t.test_files = Dir['test/**/*_test.rb']
@@ -16,7 +16,7 @@ end
1616
begin
1717
require 'rcov/rcovtask'
1818
namespace :test do
19-
desc "Test preferences with Rcov."
19+
desc 'Test preferences with Rcov.'
2020
Rcov::RcovTask.new(:rcov) do |t|
2121
t.libs << 'lib'
2222
t.test_files = Dir['test/**/*_test.rb']
@@ -27,7 +27,7 @@ begin
2727
rescue LoadError
2828
end
2929

30-
desc "Generate documentation for preferences."
30+
desc 'Generate documentation for preferences.'
3131
Rake::RDocTask.new(:rdoc) do |rdoc|
3232
rdoc.rdoc_dir = 'rdoc'
3333
rdoc.title = 'preferences'

app/models/preference.rb

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,68 @@
11
# Represents a preferred value for a particular preference on a model.
2-
#
2+
#
33
# == Grouped preferences
4-
#
4+
#
55
# In addition to simple named preferences, preferences can also be grouped by
66
# a particular value, be it a string or ActiveRecord object. For example, a
77
# User may have a preferred color for a particular Car. In this case, the
88
# +owner+ is the User record, the +name+ is "color", and the +group+ is the
99
# Car record. This allows preferences to have a sort of context around them.
1010
class Preference < ActiveRecord::Base
11-
belongs_to :owner, :polymorphic => true
12-
belongs_to :group, :polymorphic => true
13-
11+
belongs_to :owner, polymorphic: true
12+
belongs_to :group, polymorphic: true
13+
1414
validates_presence_of :name, :owner_id, :owner_type
15-
validates_presence_of :group_type, :if => :group_id?
16-
15+
validates_presence_of :group_type, if: :group_id?
16+
1717
class << self
1818
# Splits the given group into its corresponding id and type. For simple
1919
# primitives, the id will be nil. For complex types, specifically
2020
# ActiveRecord objects, the id is the unique identifier stored in the
2121
# database for the record.
22-
#
22+
#
2323
# For example,
24-
#
24+
#
2525
# Preference.split_group('google') # => [nil, "google"]
2626
# Preference.split_group(1) # => [nil, 1]
2727
# Preference.split_group(User.find(1)) # => [1, "User"]
2828
def split_group(group = nil)
2929
if group.is_a?(ActiveRecord::Base)
30-
group_id, group_type = group.id, group.class.base_class.name.to_s
30+
group_id = group.id
31+
group_type = group.class.base_class.name.to_s
3132
else
32-
group_id, group_type = nil, group.is_a?(Symbol) ? group.to_s : group
33+
group_id = nil
34+
group_type = group.is_a?(Symbol) ? group.to_s : group
3335
end
34-
36+
3537
[group_id, group_type]
3638
end
3739
end
38-
40+
3941
# The definition of the preference as defined in the owner's model
4042
def definition
4143
# Optimize number of queries to the database by only looking up the actual
4244
# owner record for STI cases when the definition can't be found in the
4345
# stored owner type class
4446
owner_type && (find_definition(owner_type.constantize) || find_definition(owner.class))
4547
end
46-
48+
4749
# Typecasts the value depending on the preference definition's declared type
4850
def value
4951
value = read_attribute(:value)
5052
value = definition.type_cast(value) if definition
5153
value
5254
end
53-
55+
5456
# Only searches for the group record if the group id is specified
5557
def group_with_optional_lookup
5658
group_id ? group_without_optional_lookup : group_type
5759
end
5860
alias_method_chain :group, :optional_lookup
59-
61+
6062
private
61-
# Finds the definition for this preference in the given owner class.
62-
def find_definition(owner_class)
63-
owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name]
64-
end
63+
64+
# Finds the definition for this preference in the given owner class.
65+
def find_definition(owner_class)
66+
owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name]
67+
end
6568
end

generators/preferences/USAGE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Usage:
22

3-
script/generate preferences
3+
rails generate preferences
44

55
This will create a migration that will add the proper table to store preferences.

generators/preferences/preferences_generator.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class CreatePreferences < ActiveRecord::Migration
2-
def self.up
2+
def change
33
create_table :preferences do |t|
4-
t.string :name, :null => false
5-
t.references :owner, :polymorphic => true, :null => false
6-
t.references :group, :polymorphic => true
4+
t.string :name, null: false
5+
t.references :owner, polymorphic: true, null: false
6+
t.references :group, polymorphic: true
77
t.string :value
88
t.timestamps
99
end
10-
add_index :preferences, [:owner_id, :owner_type, :name, :group_id, :group_type], :unique => true, :name => 'index_preferences_on_owner_and_name_and_preference'
11-
end
12-
13-
def self.down
14-
drop_table :preferences
10+
add_index :preferences, %i[owner_id owner_type name group_id group_type], unique: true, name: 'index_preferences_on_owner_and_name_and_preference'
1511
end
1612
end

init.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
require 'preferences'

0 commit comments

Comments
 (0)