Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint Ruby with StandardRb #107

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0f442b1777212eb5f0574f1d66d8692d7b42c94d
9f9fbc7e8e711fd3c3f1377c415dffba14d08d48
fa5717a68557d679b76f8a485466bd3192c06ad7
20 changes: 18 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: CI

on: [pull_request, push, workflow_dispatch]
on:
pull_request:
push:
branches:
- master
workflow_dispatch:

jobs:
specs:
Expand Down Expand Up @@ -29,7 +34,7 @@ jobs:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
LEGACY_CONNECTION_HANDLING: ${{ matrix.legacy_connection_handling }}
steps:
- uses: zendesk/checkout@v2
- uses: zendesk/checkout@v4
- name: Set up Ruby
uses: zendesk/setup-ruby@v1
with:
Expand All @@ -52,3 +57,14 @@ jobs:
echo "Some specs failed"
false
fi

lint:
runs-on: ubuntu-latest
steps:
- uses: zendesk/checkout@v4
- name: Set up Ruby
uses: zendesk/setup-ruby@v1
with:
ruby-version: "2.7"
bundler-cache: true
- run: bundle exec rake standard
9 changes: 5 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'bundler/gem_tasks'
require 'bump/tasks'
require 'rspec/core/rake_task'
require "bundler/gem_tasks"
require "bump/tasks"
require "rspec/core/rake_task"
require "standard/rake"

task default: :spec
task default: [:spec, :standard]

# Pushing to rubygems is handled by a github workflow
ENV["gem_push"] = "false"
Expand Down
21 changes: 8 additions & 13 deletions benchmark/read.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.dirname(__FILE__) + '/../test/helper')
require File.expand_path(File.dirname(__FILE__) + "/../test/helper")

class Account < ActiveRecord::Base
# Benchmark reading from an object with many settings when:
Expand All @@ -9,31 +9,28 @@ class Account < ActiveRecord::Base
property_set :benchmark_settings do
# 30 simple objects
10.times do |i|
property "float_prop_#{i}", :type => :float, :default => 3.1415
property "int_prop_#{i}", :type => :integer, :default => 22
property "string_prop_#{i}", :type => :string, :default => "Sausalito, CA"
property "float_prop_#{i}", type: :float, default: 3.1415
property "int_prop_#{i}", type: :integer, default: 22
property "string_prop_#{i}", type: :string, default: "Sausalito, CA"
end

# 10 complex
5.times do |i|
property "datetime_prop_#{i}", :type => :datetime, :default => Time.now.to_s
property "serialized_prop_#{i}", :type => :serialized, :default => { "Hello" => "There" }
property "datetime_prop_#{i}", type: :datetime, default: Time.now.to_s
property "serialized_prop_#{i}", type: :serialized, default: {"Hello" => "There"}
end

# 60 booleans
60.times do |i|
property "boolean_prop_#{i}", :type => :boolean, :default => true
property "boolean_prop_#{i}", type: :boolean, default: true
end
end

end

class BenchmarkRead < ActiveSupport::TestCase

context "property sets" do

setup do
@account = Account.create(:name => "Name")
@account = Account.create(name: "Name")
end

should "benchmark fully defined settings" do
Expand Down Expand Up @@ -65,7 +62,6 @@ class BenchmarkRead < ActiveSupport::TestCase
end
puts "Reading empty settings: #{empty_timing}ms"
end

end

def read_settings(account)
Expand All @@ -78,5 +74,4 @@ def read_settings(account)
account.benchmark_settings.boolean_prop_40?
account.benchmark_settings.boolean_prop_50?
end

end
8 changes: 4 additions & 4 deletions gemfiles/rails6.0.gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec path: '../'
gemspec path: "../"

gem 'activerecord', '~> 6.0.0'
gem 'actionpack', '~> 6.0.0'
gem "activerecord", "~> 6.0.0"
gem "actionpack", "~> 6.0.0"
gem "sqlite3", "~> 1.4"

eval_gemfile "common.rb"
8 changes: 4 additions & 4 deletions gemfiles/rails6.1.gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec path: '../'
gemspec path: "../"

gem 'activerecord', '~> 6.1.0'
gem 'actionpack', '~> 6.1.0'
gem "activerecord", "~> 6.1.0"
gem "actionpack", "~> 6.1.0"
gem "sqlite3", "~> 1.4"

eval_gemfile "common.rb"
8 changes: 4 additions & 4 deletions gemfiles/rails7.0.gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec path: '../'
gemspec path: "../"

gem 'activerecord', '~> 7.0.0'
gem 'actionpack', '~> 7.0.0'
gem "activerecord", "~> 7.0.0"
gem "actionpack", "~> 7.0.0"
gem "sqlite3", "~> 1.4"

eval_gemfile "common.rb"
10 changes: 5 additions & 5 deletions lib/property_sets.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'property_sets/property_set_model'
require 'property_sets/active_record_extension'
require 'property_sets/version'
require "property_sets/property_set_model"
require "property_sets/active_record_extension"
require "property_sets/version"

begin
require 'property_sets/action_view_extension'
require "property_sets/action_view_extension"
rescue LoadError
end

Expand All @@ -19,7 +19,7 @@ def self.ensure_property_set_class(association, owner_class_name)
unless namespace.const_defined?(const_name, false)
property_class = Class.new(parent_for_property_class(namespace, owner_class_name)) do
include PropertySets::PropertySetModel::InstanceMethods
extend PropertySets::PropertySetModel::ClassMethods
extend PropertySets::PropertySetModel::ClassMethods
end

namespace.const_set(const_name, property_class)
Expand Down
26 changes: 12 additions & 14 deletions lib/property_sets/action_view_extension.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'action_view'
require "action_view"

module ActionView
module Helpers
Expand All @@ -8,9 +8,9 @@ class PropertySetFormBuilderProxy

def initialize(property_set, template, object_name, object)
@property_set = property_set
@template = template
@object_name = object_name
@object = object
@template = template
@object_name = object_name
@object = object
end

def check_box(property, options = {}, checked_value = "1", unchecked_value = "0")
Expand All @@ -23,7 +23,7 @@ def check_box(property, options = {}, checked_value = "1", unchecked_value = "0"
def radio_button(property, checked_value = "1", options = {})
options[:id] ||= "#{object_name}_#{property_set}_#{property}_#{checked_value}"
options = prepare_options(property, options) do |properties|
properties.send("#{property}") == checked_value
properties.send(property.to_s) == checked_value
end
template.radio_button(object_name, property, checked_value, options)
end
Expand All @@ -34,7 +34,7 @@ def text_field(property, options = {})

def hidden_field(property, options = {})
options = prepare_id_name(property, options)
unless options.keys.include?(:value)
unless options.key?(:value)
options[:value] = cast_boolean(options[:object].send(property_set).send(property))
end
template.hidden_field(object_name, property, options)
Expand All @@ -43,7 +43,7 @@ def hidden_field(property, options = {})
def select(property, choices, options = {}, html_options = {})
options = prepare_id_name(property, options)
current_value = options[:object].send(property_set).send(property)
template.select("#{object_name}[#{property_set}]", property, choices, { :selected => current_value }, html_options )
template.select("#{object_name}[#{property_set}]", property, choices, {selected: current_value}, html_options)
end

private
Expand All @@ -52,9 +52,9 @@ def prepare_id_name(property, options)
throw "Invalid options type #{options.inspect}" unless options.is_a?(Hash)

options.clone.tap do |prepared_options|
prepared_options[:object] = object || fetch_target_object
prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}"
prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]"
prepared_options[:object] = object || fetch_target_object
prepared_options[:id] ||= "#{object_name}_#{property_set}_#{property}"
prepared_options[:name] = "#{object_name}[#{property_set}][#{property}]"
end
end

Expand All @@ -75,18 +75,16 @@ def prepare_options(property, options, &block)

def cast_boolean(value)
case value
when TrueClass then '1'
when FalseClass then '0'
when TrueClass then "1"
when FalseClass then "0"
else value
end
end

end

def property_set(identifier)
PropertySetFormBuilderProxy.new(identifier, @template, object_name, object)
end

end
end
end
Loading