Skip to content

Commit

Permalink
Update with PRs pluginaweek#1, pluginaweek#2, and pluginaweek#12, plu…
Browse files Browse the repository at this point in the history
…s rubocopisms
  • Loading branch information
Daniel Collins committed Jul 13, 2017
1 parent 949894e commit ca85268
Show file tree
Hide file tree
Showing 26 changed files with 1,038 additions and 875 deletions.
29 changes: 29 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
inherit_from: .rubocop_todo.yml

# Offense count: 7
Metrics/AbcSize:
Max: 54

# Offense count: 1
Metrics/CyclomaticComplexity:
Max: 8

# Offense count: 134
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 221

# Offense count: 7
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 48

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 143

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 10
74 changes: 74 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-07-13 19:06:38 +0000 using RuboCop version 0.48.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
Lint/AmbiguousBlockAssociation:
Exclude:
- 'lib/preferences.rb'

# Offense count: 2
Lint/DuplicateMethods:
Exclude:
- 'test/functional/preferences_test.rb'

# Offense count: 1
Lint/HandleExceptions:
Exclude:
- 'Rakefile'

# Offense count: 2
Lint/ShadowingOuterLocalVariable:
Exclude:
- 'lib/preferences.rb'

# Offense count: 39
Lint/UselessAssignment:
Exclude:
- 'test/functional/preferences_test.rb'

# Offense count: 4
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'generators/preferences/templates/001_create_preferences.rb'
- 'lib/generators/preferences_generator.rb'
- 'lib/preferences.rb'

# Offense count: 1
Style/DoubleNegation:
Exclude:
- 'lib/preferences/preference_definition.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: format, sprintf, percent
Style/FormatString:
Exclude:
- 'lib/generators/preferences_generator.rb'

# Offense count: 2
# Configuration parameters: AllowedVariables.
Style/GlobalVars:
Exclude:
- 'test/test_helper.rb'

# Offense count: 1
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'lib/preferences.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle, SupportedStyles.
# SupportedStyles: predicate, comparison
Style/NumericPredicate:
Exclude:
- 'spec/**/*'
- 'lib/preferences.rb'
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source "http://www.rubygems.org"
source 'http://www.rubygems.org'

gemspec
39 changes: 35 additions & 4 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,37 @@ Write method:
user.write_preference(:hot_salsa, false) # => false
user.write_preference(:language, "English") # => "English"

=== Accessible preferences

If you want a preference to be accessible via the +attributes+ method on the
model, use the +accessible_preference+ method:

class User < ActiveRecord::Base
accessible_preference :hot_salsa
accessible_preference :two_percent_milk
end

Now, you can easily update all the preferences at once from your controllers:

In the view:

- form_for @user do |f|
= f.check_box :prefers_hot_salsa
= f.check_box :prefers_two_percent_milk

In the controller:

UsersController < ApplicationController
def update
@user = User.find(params[:id])
@user.attributes = params[:user]

flash.notice = 'Saved preferences' if @user.save

render 'edit'
end
end

=== Accessing all preferences

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

user = User.find(:first)
car = Car.find(:first)

user.preferred_color = 'red', car
# user.write_preference(:color, 'red', car) # The generic way

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

user = User.find(:first)

user.preferred_color = 'red', :automobiles
user.preferred_color = 'tan', :clothing

user.preferred_color(:automobiles) # => "red"
user.preferred_color(:clothing) # => "tan"

user.preferences(:automobiles) # => {"color"=>"red"}

=== Saving preferences
Expand Down
8 changes: 4 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run all tests.'
task :default => :test
task default: :test

desc "Test preferences."
desc 'Test preferences.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.test_files = Dir['test/**/*_test.rb']
Expand All @@ -16,7 +16,7 @@ end
begin
require 'rcov/rcovtask'
namespace :test do
desc "Test preferences with Rcov."
desc 'Test preferences with Rcov.'
Rcov::RcovTask.new(:rcov) do |t|
t.libs << 'lib'
t.test_files = Dir['test/**/*_test.rb']
Expand All @@ -27,7 +27,7 @@ begin
rescue LoadError
end

desc "Generate documentation for preferences."
desc 'Generate documentation for preferences.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'preferences'
Expand Down
43 changes: 23 additions & 20 deletions app/models/preference.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,68 @@
# Represents a preferred value for a particular preference on a model.
#
#
# == Grouped preferences
#
#
# In addition to simple named preferences, preferences can also be grouped by
# a particular value, be it a string or ActiveRecord object. For example, a
# User may have a preferred color for a particular Car. In this case, the
# +owner+ is the User record, the +name+ is "color", and the +group+ is the
# Car record. This allows preferences to have a sort of context around them.
class Preference < ActiveRecord::Base
belongs_to :owner, :polymorphic => true
belongs_to :group, :polymorphic => true
belongs_to :owner, polymorphic: true
belongs_to :group, polymorphic: true

validates_presence_of :name, :owner_id, :owner_type
validates_presence_of :group_type, :if => :group_id?
validates_presence_of :group_type, if: :group_id?

class << self
# Splits the given group into its corresponding id and type. For simple
# primitives, the id will be nil. For complex types, specifically
# ActiveRecord objects, the id is the unique identifier stored in the
# database for the record.
#
#
# For example,
#
#
# Preference.split_group('google') # => [nil, "google"]
# Preference.split_group(1) # => [nil, 1]
# Preference.split_group(User.find(1)) # => [1, "User"]
def split_group(group = nil)
if group.is_a?(ActiveRecord::Base)
group_id, group_type = group.id, group.class.base_class.name.to_s
group_id = group.id
group_type = group.class.base_class.name.to_s
else
group_id, group_type = nil, group.is_a?(Symbol) ? group.to_s : group
group_id = nil
group_type = group.is_a?(Symbol) ? group.to_s : group
end

[group_id, group_type]
end
end

# The definition of the preference as defined in the owner's model
def definition
# Optimize number of queries to the database by only looking up the actual
# owner record for STI cases when the definition can't be found in the
# stored owner type class
owner_type && (find_definition(owner_type.constantize) || find_definition(owner.class))
end

# Typecasts the value depending on the preference definition's declared type
def value
value = read_attribute(:value)
value = definition.type_cast(value) if definition
value
end

# Only searches for the group record if the group id is specified
def group_with_optional_lookup
group_id ? group_without_optional_lookup : group_type
end
alias_method_chain :group, :optional_lookup

private
# Finds the definition for this preference in the given owner class.
def find_definition(owner_class)
owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name]
end

# Finds the definition for this preference in the given owner class.
def find_definition(owner_class)
owner_class.respond_to?(:preference_definitions) && owner_class.preference_definitions[name]
end
end
2 changes: 1 addition & 1 deletion generators/preferences/USAGE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Usage:

script/generate preferences
rails generate preferences

This will create a migration that will add the proper table to store preferences.
7 changes: 0 additions & 7 deletions generators/preferences/preferences_generator.rb

This file was deleted.

14 changes: 5 additions & 9 deletions generators/preferences/templates/001_create_preferences.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
class CreatePreferences < ActiveRecord::Migration
def self.up
def change
create_table :preferences do |t|
t.string :name, :null => false
t.references :owner, :polymorphic => true, :null => false
t.references :group, :polymorphic => true
t.string :name, null: false
t.references :owner, polymorphic: true, null: false
t.references :group, polymorphic: true
t.string :value
t.timestamps
end
add_index :preferences, [:owner_id, :owner_type, :name, :group_id, :group_type], :unique => true, :name => 'index_preferences_on_owner_and_name_and_preference'
end

def self.down
drop_table :preferences
add_index :preferences, %i[owner_id owner_type name group_id group_type], unique: true, name: 'index_preferences_on_owner_and_name_and_preference'
end
end
1 change: 0 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
require 'preferences'
17 changes: 17 additions & 0 deletions lib/generators/preferences_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class PreferencesGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime('%Y%m%d%H%M%S')
else
'%.3d' % (current_migration_number(dirname) + 1)
end
end

def create_migration_file
migration_template 'create_preferences.rb', 'db/migrate/create_preferences.rb'
end
end
Loading

0 comments on commit ca85268

Please sign in to comment.