forked from pluginaweek/preferences
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…s rubocopisms
- Loading branch information
Daniel Collins
committed
Jul 13, 2017
1 parent
949894e
commit ca85268
Showing
26 changed files
with
1,038 additions
and
875 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
require 'preferences' | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.