chore: rubocop

chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
This commit is contained in:
Philipp Rothmann 2023-05-12 13:01:12 +02:00 committed by Philipp Rothmann
parent f6fb804bbe
commit fb2b4d8a8a
331 changed files with 4263 additions and 4507 deletions

View file

@ -20,7 +20,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
load 'rails/tasks/engine.rake'
Bundler::GemHelper.install_tasks
@ -34,4 +34,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end
task :default => :test
task default: :test

View file

@ -1,20 +1,20 @@
class PagesController < ApplicationController
before_action -> { require_plugin_enabled FoodsoftWiki }
before_action :catch_special_pages, only: [:show, :new]
before_action :catch_special_pages, only: %i[show new]
skip_before_action :authenticate, :only => :all
before_action :only => :all do
authenticate_or_token(['wiki', 'all'])
skip_before_action :authenticate, only: :all
before_action only: :all do
authenticate_or_token(%w[wiki all])
end
before_action do
content_for :head, view_context.rss_meta_tag
end
def index
@page = Page.find_by_permalink "Home"
@page = Page.find_by_permalink 'Home'
if @page
render :action => 'show'
render action: 'show'
else
redirect_to all_pages_path
end
@ -34,11 +34,11 @@ class PagesController < ApplicationController
end
if @page.nil?
redirect_to new_page_path(:title => params[:permalink])
redirect_to new_page_path(title: params[:permalink])
elsif @page.redirect?
page = Page.find_by_id(@page.redirect)
unless page.nil?
flash[:notice] = I18n.t('pages.cshow.redirect_notice', :page => @page.title)
flash[:notice] = I18n.t('pages.cshow.redirect_notice', page: @page.title)
redirect_to wiki_page_path(page.permalink)
end
end
@ -46,12 +46,12 @@ class PagesController < ApplicationController
def new
@page = Page.new
@page.title = params[:title].gsub("_", " ") if params[:title]
@page.title = params[:title].gsub('_', ' ') if params[:title]
@page.parent = Page.find_by_permalink(params[:parent]) if params[:parent]
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @page }
format.xml { render xml: @page }
end
end
@ -60,36 +60,32 @@ class PagesController < ApplicationController
end
def create
@page = Page.new(params[:page].merge({ :user => current_user }))
@page = Page.new(params[:page].merge({ user: current_user }))
if params[:preview]
render :action => 'new'
render action: 'new'
elsif @page.save
flash[:notice] = I18n.t('pages.create.notice')
redirect_to(wiki_page_path(@page.permalink))
else
if @page.save
flash[:notice] = I18n.t('pages.create.notice')
redirect_to(wiki_page_path(@page.permalink))
else
render :action => "new"
end
render action: 'new'
end
end
def update
@page = Page.find(params[:id])
@page.attributes = params[:page].merge({ :user => current_user })
@page.attributes = params[:page].merge({ user: current_user })
if params[:preview]
@page.attributes = params[:page]
render :action => 'edit'
render action: 'edit'
elsif @page.save
@page.parent_id = parent_id if params[:parent_id].present? \
&& params[:parent_id] != @page_id
flash[:notice] = I18n.t('pages.update.notice')
redirect_to wiki_page_path(@page.permalink)
else
if @page.save
@page.parent_id = parent_id if (!params[:parent_id].blank? \
&& params[:parent_id] != @page_id)
flash[:notice] = I18n.t('pages.update.notice')
redirect_to wiki_page_path(@page.permalink)
else
render :action => "edit"
end
render action: 'edit'
end
rescue ActiveRecord::StaleObjectError
flash[:error] = I18n.t('pages.error_stale_object')
@ -100,7 +96,7 @@ class PagesController < ApplicationController
@page = Page.find(params[:id])
@page.destroy
flash[:notice] = I18n.t('pages.destroy.notice', :page => @page.title)
flash[:notice] = I18n.t('pages.destroy.notice', page: @page.title)
redirect_to wiki_path
end
@ -109,23 +105,23 @@ class PagesController < ApplicationController
@partial = params[:view] || 'site_map'
if params[:name]
@pages = @pages.where("title LIKE ?", "%#{params[:name]}%").limit(20)
@pages = @pages.where('title LIKE ?', "%#{params[:name]}%").limit(20)
@partial = 'title_list'
end
if params[:sort]
sort = case params[:sort]
when "title" then "title"
when "title_reverse" then "title DESC"
when "last_updated" then "updated_at DESC"
when "last_updated_reverse" then "updated_at"
sort = if params[:sort]
case params[:sort]
when 'title' then 'title'
when 'title_reverse' then 'title DESC'
when 'last_updated' then 'updated_at DESC'
when 'last_updated_reverse' then 'updated_at'
end
else
sort = "title"
end
else
'title'
end
@pages = @pages.order(sort)
respond_to do |format|
format.html
format.rss { render :layout => false }
format.rss { render layout: false }
end
end
@ -150,15 +146,15 @@ class PagesController < ApplicationController
def variables
keys = Foodsoft::ExpansionVariables.variables.keys
@variables = Hash[keys.map { |k| [k, Foodsoft::ExpansionVariables.get(k)] }]
@variables = keys.index_with { |k| Foodsoft::ExpansionVariables.get(k) }
render 'variables'
end
private
def catch_special_pages
if params[:id] == 'Help:Foodsoft_variables'
variables
end
return unless params[:id] == 'Help:Foodsoft_variables'
variables
end
end

View file

@ -2,70 +2,70 @@ module PagesHelper
include WikiCloth
def rss_meta_tag
tag.link(rel: "alternate", type: "application/rss+xml", title: "RSS", href: all_pages_rss_url).html_safe
tag.link(rel: 'alternate', type: 'application/rss+xml', title: 'RSS', href: all_pages_rss_url).html_safe
end
def wikified_body(body, title = nil)
FoodsoftWiki::WikiParser.new(data: body + "\n", params: { referer: title }).to_html(noedit: true).html_safe
rescue => e
rescue StandardError => e
# try the following with line breaks: === one === == two == = three =
content_tag :span, class: 'alert alert-error' do
I18n.t '.wikicloth_exception', :msg => e
I18n.t '.wikicloth_exception', msg: e
end.html_safe
end
def link_to_wikipage(page, text = nil)
if text == nil
link_to page.title, wiki_page_path(:permalink => page.permalink)
if text.nil?
link_to page.title, wiki_page_path(permalink: page.permalink)
else
link_to text, wiki_page_path(:permalink => page.permalink)
link_to text, wiki_page_path(permalink: page.permalink)
end
end
def link_to_wikipage_by_permalink(permalink, text = nil)
unless permalink.blank?
page = Page.find_by_permalink(permalink)
if page.nil?
if text.nil?
link_to permalink, new_page_path(:title => permalink)
else
link_to text, new_page_path(:title => permalink)
end
return if permalink.blank?
page = Page.find_by_permalink(permalink)
if page.nil?
if text.nil?
link_to permalink, new_page_path(title: permalink)
else
link_to_wikipage(page, text)
link_to text, new_page_path(title: permalink)
end
else
link_to_wikipage(page, text)
end
end
def generate_toc(body)
toc = String.new
body.gsub(/^([=]{1,6})\s*(.*?)\s*(\1)/) do
number = $1.length - 1
name = $2
body.gsub(/^(={1,6})\s*(.*?)\s*(\1)/) do
number = ::Regexp.last_match(1).length - 1
name = ::Regexp.last_match(2)
toc << "*" * number + " #{name}\n"
toc << (('*' * number) + " #{name}\n")
end
unless toc.blank?
FoodsoftWiki::WikiParser.new(data: toc).to_html.gsub(/<li>([^<>\n]*)/) do
name = $1
anchor = name.gsub(/\s/, '_').gsub(/[^a-zA-Z_]/, '')
"<li><a href='##{anchor}'>#{name.truncate(20)}</a>"
end.html_safe
end
return if toc.blank?
FoodsoftWiki::WikiParser.new(data: toc).to_html.gsub(/<li>([^<>\n]*)/) do
name = ::Regexp.last_match(1)
anchor = name.gsub(/\s/, '_').gsub(/[^a-zA-Z_]/, '')
"<li><a href='##{anchor}'>#{name.truncate(20)}</a>"
end.html_safe
end
def parent_pages_to_select(current_page)
unless current_page.homepage? # Homepage is the page trees root!
if current_page.homepage?
[]
else # Homepage is the page trees root!
Page.non_redirected.reject { |p| p == current_page || p.ancestors.include?(current_page) }
else
Array.new
end
end
# return url for all_pages rss feed
def all_pages_rss_url(options = {})
token = TokenVerifier.new(['wiki', 'all']).generate
all_pages_url({ :format => 'rss', :token => token }.merge(options))
token = TokenVerifier.new(%w[wiki all]).generate
all_pages_url({ format: 'rss', token: token }.merge(options))
end
end

View file

@ -1,61 +1,62 @@
class Page < ApplicationRecord
include ActsAsTree
belongs_to :user, :foreign_key => 'updated_by'
belongs_to :user, foreign_key: 'updated_by'
acts_as_versioned version_column: :lock_version
self.non_versioned_columns += %w(permalink created_at title)
self.non_versioned_columns += %w[permalink created_at title]
acts_as_tree :order => "title"
acts_as_tree order: 'title'
attr_accessor :old_title # Save title to create redirect page when editing title
validates_presence_of :title, :body
validates_uniqueness_of :permalink, :title
before_validation :set_permalink, :on => :create
before_validation :update_permalink, :on => :update
before_validation :set_permalink, on: :create
before_validation :update_permalink, on: :update
after_update :create_redirect
scope :non_redirected, -> { where(:redirect => nil) }
scope :no_parent, -> { where(:parent_id => nil) }
scope :non_redirected, -> { where(redirect: nil) }
scope :no_parent, -> { where(parent_id: nil) }
def self.permalink(title)
title.gsub(/[\/\.,;@\s]/, "_").gsub(/[\"\']/, "")
title.gsub(%r{[/.,;@\s]}, '_').gsub(/["']/, '')
end
def homepage?
permalink == "Home"
permalink == 'Home'
end
def self.dashboard
where(permalink: "Dashboard").first
where(permalink: 'Dashboard').first
end
def self.public_front_page
where(permalink: "Public_frontpage").first
where(permalink: 'Public_frontpage').first
end
def self.welcome_mail
where(permalink: "Welcome_mail").first
where(permalink: 'Welcome_mail').first
end
def set_permalink
unless title.blank?
self.permalink = Page.count == 0 ? "Home" : Page.permalink(title)
end
return if title.blank?
self.permalink = Page.count == 0 ? 'Home' : Page.permalink(title)
end
def diff
current = versions.latest
old = versions.where(["page_id = ? and lock_version < ?", current.page_id, current.lock_version]).order('lock_version DESC').first
old = versions.where(['page_id = ? and lock_version < ?', current.page_id,
current.lock_version]).order('lock_version DESC').first
if old
o = ''
Diffy::Diff.new(old.body, current.body).each do |line|
case line
when /^\+/ then o += "#{line.chomp}<br />" unless line.chomp == "+"
when /^-/ then o += "#{line.chomp}<br />" unless line.chomp == "-"
when /^\+/ then o += "#{line.chomp}<br />" unless line.chomp == '+'
when /^-/ then o += "#{line.chomp}<br />" unless line.chomp == '-'
end
end
o
@ -67,19 +68,19 @@ class Page < ApplicationRecord
protected
def update_permalink
if changed.include?("title")
set_permalink
self.old_title = changes["title"].first # Save title for creating redirect
end
return unless changed.include?('title')
set_permalink
self.old_title = changes['title'].first # Save title for creating redirect
end
def create_redirect
unless old_title.blank?
Page.create :redirect => id,
:title => old_title,
:body => I18n.t('model.page.redirect', :title => title),
:permalink => Page.permalink(old_title),
:updated_by => updated_by
end
return if old_title.blank?
Page.create redirect: id,
title: old_title,
body: I18n.t('model.page.redirect', title: title),
permalink: Page.permalink(old_title),
updated_by: updated_by
end
end

View file

@ -1,16 +1,16 @@
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.instruct! :xml, version: '1.0'
xml.rss version: '2.0' do
xml.channel do
xml.title FoodsoftConfig[:name] + " Wiki"
xml.description ""
xml.title FoodsoftConfig[:name] + ' Wiki'
xml.description ''
xml.link FoodsoftConfig[:homepage]
for page in @pages
xml.item do
xml.title page.title
xml.description page.diff, :type => "html"
xml.description page.diff, type: 'html'
xml.author User.find_by_id(page.updated_by).try(:display)
xml.pubDate page.updated_at.to_s(:rfc822)
xml.pubDate page.updated_at.to_fs(:rfc822)
xml.link wiki_page_path(page.permalink)
xml.guid page.updated_at.to_i
end

View file

@ -1,12 +1,12 @@
Rails.application.routes.draw do
scope '/:foodcoop' do
resources :pages do
get :all, :on => :collection
get :version, :on => :member
get :revert, :on => :member
get :diff, :on => :member
get :all, on: :collection
get :version, on: :member
get :revert, on: :member
get :diff, on: :member
end
get '/wiki/:permalink' => 'pages#show', :as => 'wiki_page' # , :constraints => {:permalink => /[^\s]+/}
get '/wiki' => 'pages#show', :defaults => { :permalink => 'Home' }, :as => 'wiki'
get '/wiki' => 'pages#show', :defaults => { permalink: 'Home' }, :as => 'wiki'
end
end

View file

@ -4,7 +4,7 @@ class CreatePages < ActiveRecord::Migration[4.2]
t.string :title
t.text :body
t.string :permalink
t.integer :lock_version, :default => 0
t.integer :lock_version, default: 0
t.integer :updated_by
t.integer :redirect
t.integer :parent_id

View file

@ -1,26 +1,27 @@
$:.push File.expand_path("../lib", __FILE__)
$:.push File.expand_path('lib', __dir__)
# Maintain your gem's version:
require "foodsoft_wiki/version"
require 'foodsoft_wiki/version'
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "foodsoft_wiki"
s.name = 'foodsoft_wiki'
s.version = FoodsoftWiki::VERSION
s.authors = ["wvengen"]
s.email = ["dev-foodsoft@willem.engen.nl"]
s.homepage = "https://github.com/foodcoops/foodsoft"
s.summary = "Wiki plugin for foodsoft."
s.description = "Adds a wiki to foodsoft."
s.authors = ['wvengen']
s.email = ['dev-foodsoft@willem.engen.nl']
s.homepage = 'https://github.com/foodcoops/foodsoft'
s.summary = 'Wiki plugin for foodsoft.'
s.description = 'Adds a wiki to foodsoft.'
s.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "README.md"]
s.files = Dir['{app,config,db,lib}/**/*'] + ['Rakefile', 'README.md']
s.add_dependency "rails"
s.add_dependency 'rails'
s.add_dependency 'wikicloth'
s.add_dependency 'twitter-text', '~> 1.14' # wikicloth doesn't support version 2
s.add_dependency 'acts_as_versioned' # need git version, make sure that is included in foodsoft's Gemfile
s.add_dependency "deface", "~> 1.0"
s.add_dependency 'deface', '~> 1.0'
s.add_dependency 'diffy'
s.add_dependency 'content_for_in_controllers'
s.add_development_dependency "sqlite3"
s.add_development_dependency 'sqlite3'
s.metadata['rubygems_mfa_required'] = 'true'
end

View file

@ -8,17 +8,17 @@ module FoodsoftWiki
subnav.item :all_pages, I18n.t('navigation.wiki.all_pages'), ctx.all_pages_path, id: nil
end
# move this last added item to just after the foodcoop menu
if i = primary.items.index(primary[:foodcoop])
primary.items.insert(i + 1, primary.items.delete_at(-1))
end
return unless i = primary.items.index(primary[:foodcoop])
primary.items.insert(i + 1, primary.items.delete_at(-1))
end
def default_foodsoft_config(cfg)
cfg[:use_wiki] = true
end
initializer "foodsoft_wiki.assets.precompile" do |app|
app.config.assets.precompile += %w(icons/feed-icon-14x14.png)
initializer 'foodsoft_wiki.assets.precompile' do |app|
app.config.assets.precompile += %w[icons/feed-icon-14x14.png]
end
end
end

View file

@ -3,10 +3,10 @@ module FoodsoftWiki
def self.included(base) # :nodoc:
base.class_eval do
# modify user presentation link to writing a message for the user
def additonal_welcome_text(user)
if FoodsoftWiki.enabled? && (page = Page.welcome_mail)
page.body
end
def additonal_welcome_text(_user)
return unless FoodsoftWiki.enabled? && (page = Page.welcome_mail)
page.body
end
end
end
@ -15,5 +15,5 @@ end
# modify existing helper
ActiveSupport.on_load(:after_initialize) do
Mailer.send :include, FoodsoftWiki::Mailer
Mailer.include FoodsoftWiki::Mailer
end

View file

@ -1,3 +1,3 @@
module FoodsoftWiki
VERSION = "0.0.1"
VERSION = '0.0.1'
end

View file

@ -10,7 +10,7 @@ module FoodsoftWiki
link_attributes_for do |page|
permalink = Page.permalink(page)
if Page.exists?(:permalink => permalink)
if Page.exists?(permalink: permalink)
{ href: url_for(:wiki_page_path, permalink: permalink) }
elsif page.include? '#'
# If "Foo#Bar" does not exist then consider "Foo" with anchor.
@ -20,8 +20,8 @@ module FoodsoftWiki
end
end
section_link do |section|
""
section_link do |_section|
''
end
def to_html(render_options = {})
@ -41,7 +41,7 @@ module FoodsoftWiki
return { href: '#' + anchor } if page.empty?
permalink = Page.permalink(page)
if Page.exists?(:permalink => permalink)
if Page.exists?(permalink: permalink)
{ href: url_for(:wiki_page_path, permalink: permalink, anchor: anchor) }
else
# Do not suggest to use number signs in the title.