[dw-free] Refactor DW::Setting::View*Style
[commit: http://hg.dwscoalition.org/dw-free/rev/7c44db11e9d7]
http://bugs.dwscoalition.org/show_bug.cgi?id=4300
Refactor! Create new base modules DW::Setting::JournalStyle (for
::JournalEntryStyle) and DW::Setting::ViewStyle (base for ::ViewEntryStyle
and ::ViewJournalStyle) to reduce redundancy, and effort, since we have
several very similar options that don't vary in logic.
Patch by
exor674.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=4300
Refactor! Create new base modules DW::Setting::JournalStyle (for
::JournalEntryStyle) and DW::Setting::ViewStyle (base for ::ViewEntryStyle
and ::ViewJournalStyle) to reduce redundancy, and effort, since we have
several very similar options that don't vary in logic.
Patch by
Files modified:
- cgi-bin/DW/Setting/JournalEntryStyle.pm
- cgi-bin/DW/Setting/JournalStyle.pm
- cgi-bin/DW/Setting/ViewEntryStyle.pm
- cgi-bin/DW/Setting/ViewJournalStyle.pm
- cgi-bin/DW/Setting/ViewStyle.pm
- cgi-bin/LJ/User.pm
--------------------------------------------------------------------------------
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/DW/Setting/JournalEntryStyle.pm
--- a/cgi-bin/DW/Setting/JournalEntryStyle.pm Fri Feb 10 21:14:02 2012 +0800
+++ b/cgi-bin/DW/Setting/JournalEntryStyle.pm Fri Feb 10 22:43:21 2012 +0800
@@ -6,9 +6,10 @@
# viewing the user's own journal - the selected S2 style or the site style.
#
# Authors:
-# Jen Griffin <kareila@livejournal.com>
+# Jen Griffin <kareila@livejournal.com>
+# Andrea Nall <anall@andreanall.com>
#
-# Copyright (c) 2011 by Dreamwidth Studios, LLC.
+# Copyright (c) 2011-2012 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself. For a copy of the license, please reference
@@ -16,49 +17,31 @@
#
package DW::Setting::JournalEntryStyle;
-use base 'LJ::Setting';
+use base 'DW::Setting::JournalStyle';
use strict;
use LJ::S2;
-sub should_render {
- my ( $class, $u ) = @_;
-
- return 0 unless $u;
- return $u->is_syndicated ? 0 : 1;
+sub label {
+ return $_[0]->ml( 'setting.display.journalentrystyle.label' );
}
-sub label {
- my ( $class, $u ) = @_;
-
- return $class->ml( 'setting.display.journalentrystyle.label' );
+sub option_ml {
+ return $_[0]->ml('setting.display.journalentrystyle.option');
}
-sub option {
- my ( $class, $u, $errs, $args ) = @_;
- my $key = $class->pkgkey;
-
- my $use_s2 = $class->get_arg( $args, "journalentrystyle" ) ||
- LJ::S2::use_journalstyle_entry_page( $u );
-
- my $ret = LJ::html_check( {
- name => "${key}journalentrystyle",
- id => "${key}journalentrystyle",
- value => 1,
- selected => $use_s2 ? 1 : 0,
- } );
- $ret .= " <label for='${key}journalentrystyle'>" . $class->ml('setting.display.journalentrystyle.option') . "</label>";
- $ret .= "<br /><i>" . $class->ml('setting.display.journalentrystyle.note') . "</i>";
-
- return $ret;
+sub note_ml {
+ return $_[0]->ml('setting.display.journalentrystyle.note');
+}
+sub current_value {
+ return LJ::S2::use_journalstyle_entry_page( $_[1] );
}
-sub save {
- my ( $class, $u, $args ) = @_;
+sub prop_name {
+ return 'use_journalstyle_entry_page';
+}
- my $val = $class->get_arg( $args, "journalentrystyle" ) ? "Y" : "N";
- $u->set_prop( use_journalstyle_entry_page => $val );
-
+sub store_negative {
return 1;
}
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/DW/Setting/JournalStyle.pm
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Setting/JournalStyle.pm Fri Feb 10 22:43:21 2012 +0800
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+#
+# DW::Setting::JournalStyle
+#
+# LJ::Setting module for specifying which view is displayed by default when
+# viewing the user's own journal - the selected S2 style or the site style.
+#
+# Authors:
+# Jen Griffin <kareila@livejournal.com>
+# Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2011-2012 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself. For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+
+package DW::Setting::JournalStyle;
+use base 'LJ::Setting';
+use strict;
+
+# Only override the below methods
+
+sub label {
+ die "Neglected to override 'label' in DW::Setting::JournalStyle subclass";
+}
+
+sub option_ml {
+ die "Neglected to override 'option_ml' in DW::Setting::JournalStyle subclass";
+}
+
+sub note_ml {
+ return undef;
+}
+
+sub prop_name {
+ die "Neglected to override 'prop_name' in DW::Setting::JournalStyle subclass";
+}
+
+sub current_value {
+ return $_[1]->prop( $_[0]->prop_name );
+}
+
+sub store_negative {
+ return 0;
+}
+
+# Do not override any of these
+
+sub should_render {
+ my ( $class, $u ) = @_;
+
+ return 0 unless $u;
+ return $u->is_syndicated ? 0 : 1;
+}
+
+sub option {
+ my ( $class, $u, $errs, $args ) = @_;
+ my $key = $class->pkgkey;
+
+ my $use_s2 = $class->get_arg( $args, "style" ) ||
+ $class->current_value( $u );
+
+ my $ret = LJ::html_check( {
+ name => "${key}style",
+ id => "${key}style",
+ value => 1,
+ selected => $use_s2 ? 1 : 0,
+ } );
+ $ret .= " <label for='${key}style'>" . $class->option_ml . "</label>";
+
+ my $note = $class->note_ml;
+ $ret .= "<br /><i>$note</i>" if $note;
+
+ return $ret;
+}
+
+sub save {
+ my ( $class, $u, $args ) = @_;
+
+ my $name = $class->prop_name;
+ my $value = $class->get_arg( $args, "style" );
+ my $out = undef;
+
+ if ( $class->store_negative) {
+ $out = $value ? 'Y' : 'N';
+ } elsif ( $value ) {
+ $out = 'Y';
+ }
+
+ $u->set_prop( $name => $out );
+
+ return 1;
+}
+
+1;
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/DW/Setting/ViewEntryStyle.pm
--- a/cgi-bin/DW/Setting/ViewEntryStyle.pm Fri Feb 10 21:14:02 2012 +0800
+++ b/cgi-bin/DW/Setting/ViewEntryStyle.pm Fri Feb 10 22:43:21 2012 +0800
@@ -7,8 +7,9 @@
#
# Authors:
# foxfirefey <foxfirefey@dreamwidth.org>
+# Andrea Nall <anall@andreanall.com>
#
-# Copyright (c) 2010 by Dreamwidth Studios, LLC.
+# Copyright (c) 2010-2012 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself. For a copy of the license, please reference
@@ -16,66 +17,24 @@
#
package DW::Setting::ViewEntryStyle;
-use base 'LJ::Setting';
+use base 'DW::Setting::ViewStyle';
use strict;
use warnings;
-sub should_render {
- my ( $class, $u ) = @_;
-
- return 0 unless $u;
- return $u->is_community ? 0 : 1;
+sub supports_site {
+ return 1;
}
sub label {
- my ( $class, $u ) = @_;
-
- return $class->ml( 'setting.display.viewentrystyle.label' );
+ return $_[0]->ml( 'setting.display.viewentrystyle.label' );
}
-sub option {
- my ( $class, $u, $errs, $args ) = @_;
- my $key = $class->pkgkey;
-
- my $viewentrystyle = $class->get_arg( $args, "viewentrystyle" ) || $u->opt_viewentrystyle;
-
- my @options = (
- O => $class->ml( 'setting.display.viewstyle.original' ),
- S => $class->ml( 'setting.display.viewstyle.site' ),
- M => $class->ml( 'setting.display.viewstyle.mine' ),
- L => $class->ml( 'setting.display.viewstyle.light' ),
- );
-
- my $ret = "<label for='${key}viewentrystyle'>" . $class->ml( 'setting.display.viewentrystyle.option' ) . "</label> ";
- $ret .= LJ::html_select({
- name => "${key}viewentrystyle",
- id => "${key}viewentrystyle",
- selected => $viewentrystyle,
- }, @options );
-
- return $ret;
+sub option_ml {
+ return $_[0]->ml( 'setting.display.viewentrystyle.option' );
}
-sub error_check {
- my ( $class, $u, $args ) = @_;
- my $val = $class->get_arg( $args, "viewentrystyle" );
- $class->errors( viewentrystyle => $class->ml( '.setting.display.viewstyle.invalid' ) ) unless $val =~ /^[OSML]$/;
- return 1;
-}
-
-sub save {
- my ( $class, $u, $args ) = @_;
- $class->error_check( $u, $args );
-
- my $val = $class->get_arg( $args, "viewentrystyle" );
-
- # don't save if this is the value we are already using
- return 1 if $u->prop( 'opt_viewentrystyle' ) and $val eq $u->prop( 'opt_viewentrystyle' );
-
- # delete if we are turning it back to the default
- $val = "" if $val eq "O";
-
- $u->set_prop( "opt_viewentrystyle", $val );
+sub prop_name {
+ return 'opt_viewentrystyle';
}
1;
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/DW/Setting/ViewJournalStyle.pm
--- a/cgi-bin/DW/Setting/ViewJournalStyle.pm Fri Feb 10 21:14:02 2012 +0800
+++ b/cgi-bin/DW/Setting/ViewJournalStyle.pm Fri Feb 10 22:43:21 2012 +0800
@@ -7,8 +7,9 @@
#
# Authors:
# foxfirefey <foxfirefey@dreamwidth.org>
+# Andrea Nall <anall@andreanall.com>
#
-# Copyright (c) 2010 by Dreamwidth Studios, LLC.
+# Copyright (c) 2010-2012 by Dreamwidth Studios, LLC.
#
# This program is free software; you may redistribute it and/or modify it under
# the same terms as Perl itself. For a copy of the license, please reference
@@ -16,67 +17,24 @@
#
package DW::Setting::ViewJournalStyle;
-use base 'LJ::Setting';
+use base 'DW::Setting::ViewStyle';
use strict;
use warnings;
-sub should_render {
- my ( $class, $u ) = @_;
-
- return 0 unless $u;
- return $u->is_community ? 0 : 1;
+sub supports_site {
+ return 0;
}
sub label {
- my ( $class, $u ) = @_;
-
- return $class->ml( 'setting.display.viewjournalstyle.label' );
+ return $_[0]->ml( 'setting.display.viewjournalstyle.label' );
}
-sub option {
- my ( $class, $u, $errs, $args ) = @_;
- my $key = $class->pkgkey;
-
- my $viewjournalstyle = $class->get_arg( $args, "viewjournalstyle" ) || $u->opt_viewjournalstyle;
-
- my @options = (
- O => $class->ml( 'setting.display.viewstyle.original' ),
- M => $class->ml( 'setting.display.viewstyle.mine' ),
- # Todo: make this a viable option! Needs some styling.
- #S => $class->ml( 'setting.display.viewstyle.site' ),
- L => $class->ml( 'setting.display.viewstyle.light' ),
- );
-
- my $ret = "<label for='${key}viewjournalstyle'>" . $class->ml( 'setting.display.viewjournalstyle.option' ) . "</label> ";
- $ret .= LJ::html_select({
- name => "${key}viewjournalstyle",
- id => "${key}viewjournalstyle",
- selected => $viewjournalstyle,
- }, @options );
-
- return $ret;
+sub option_ml {
+ return $_[0]->ml( 'setting.display.viewjournalstyle.option' );
}
-sub error_check {
- my ( $class, $u, $args ) = @_;
- my $val = $class->get_arg( $args, "viewjournalstyle" );
- $class->errors( viewjournalstyle => $class->ml( '.setting.display.viewstyle.invalid' ) ) unless $val =~ /^[OSML]$/;
- return 1;
-}
-
-sub save {
- my ( $class, $u, $args ) = @_;
- $class->error_check( $u, $args );
-
- my $val = $class->get_arg( $args, "viewjournalstyle" );
-
- # don't save if this is the value we are already using
- return 1 if $u->prop( 'opt_viewjournalstyle' ) and $val eq $u->prop( 'opt_viewjournalstyle' );
-
- # delete if we are turning it back to the default
- $val = "" if $val eq "O";
-
- $u->set_prop( "opt_viewjournalstyle", $val );
+sub prop_name {
+ return 'opt_viewjournalstyle';
}
1;
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/DW/Setting/ViewStyle.pm
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Setting/ViewStyle.pm Fri Feb 10 22:43:21 2012 +0800
@@ -0,0 +1,108 @@
+#!/usr/bin/perl
+#
+# DW::Setting::ViewStyle
+#
+# Generic LJ::Setting module for specifying what style journal views are
+# displayed in for a user.
+#
+# Authors:
+# foxfirefey <foxfirefey@dreamwidth.org>
+# Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2010-2012 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself. For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+
+package DW::Setting::ViewStyle;
+use base 'LJ::Setting';
+use strict;
+use warnings;
+
+# Only override the below methods
+
+sub supports_site {
+ return 0;
+}
+
+sub label {
+ die "Neglected to override 'label' in DW::Setting::ViewStyle subclass";
+}
+
+sub option_ml {
+ die "Neglected to override 'option_ml' in DW::Setting::ViewStyle subclass";
+}
+
+sub prop_name {
+ die "Neglected to override 'prop_name' in DW::Setting::ViewStyle subclass";
+}
+
+# Do not override any of these
+
+sub should_render {
+ my ( $class, $u ) = @_;
+
+ return 0 unless $u;
+ return $u->is_community ? 0 : 1;
+}
+
+sub option {
+ my ( $class, $u, $errs, $args ) = @_;
+ my $key = $class->pkgkey;
+ my $name = $class->prop_name;
+
+ my $viewjournalstyle = $class->get_arg( $args, "viewjournalstyle" ) || $u->prop( $name ) || 'O';
+
+ my @options = (
+ O => $class->ml( 'setting.display.viewstyle.original' ),
+ M => $class->ml( 'setting.display.viewstyle.mine' ),
+ );
+
+ push @options, (
+ S => $class->ml( 'setting.display.viewstyle.site' ),
+ ) if $class->supports_site;
+
+ push @options, (
+ L => $class->ml( 'setting.display.viewstyle.light' ),
+ );
+
+ my $ret = "<label for='${key}style'>" . $class->option_ml . "</label> ";
+ $ret .= LJ::html_select({
+ name => "${key}style",
+ id => "${key}style",
+ selected => $viewjournalstyle,
+ }, @options );
+
+ return $ret;
+}
+
+sub error_check {
+ my ( $class, $u, $args ) = @_;
+ my $val = uc( $class->get_arg( $args, "style" ) );
+
+ $class->error( style => $class->ml( '.setting.display.viewstyle.invalid' ) ) unless $val =~ /^[OMSL]$/;
+ $class->error( style => $class->ml( '.setting.display.viewstyle.invalid' ) ) if ( $val eq 'S' && ! $class->supports_site );
+
+ return 1;
+}
+
+sub save {
+ my ( $class, $u, $args ) = @_;
+ $class->error_check( $u, $args );
+
+ my $name = $class->prop_name;
+
+ my $val = $class->get_arg( $args, "style" );
+
+ # don't save if this is the value we are already using
+ return 1 if $u->prop( $name ) and $val eq $u->prop( $name );
+
+ # delete if we are turning it back to the default
+ $val = "" if $val eq "O";
+
+ $u->set_prop( $name, $val );
+}
+
+1;
diff -r d9889466f416 -r 7c44db11e9d7 cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm Fri Feb 10 21:14:02 2012 +0800
+++ b/cgi-bin/LJ/User.pm Fri Feb 10 22:43:21 2012 +0800
@@ -5913,14 +5913,6 @@
}
}
-sub opt_viewentrystyle {
- return ( $_[0]->prop( 'opt_viewentrystyle' ) || "O" );
-}
-
-sub opt_viewjournalstyle {
- return ( $_[0]->prop( 'opt_viewjournalstyle' ) || "O" );
-}
-
sub set_default_style {
my $style = eval { LJ::Customize->verify_and_load_style( $_[0] ); };
warn $@ if $@;
@@ -6001,16 +5993,13 @@
$view ||= 'entry';
my %style_types = ( O => "original", M => "mine", S => "site", L => "light" );
-
- my $style;
- if ( $view eq "entry" || $view eq "reply" ) {
- $style = $style_types{ $u->opt_viewentrystyle };
- } else {
- $style = $style_types{ $u->opt_viewjournalstyle };
- }
- $style ||= 'original';
-
- return $style;
+ my %view_props = (
+ entry => 'opt_viewentrystyle',
+ reply => 'opt_viewentrystyle',
+ );
+
+ my $prop = $view_props{ $view } || 'opt_viewjournalstyle';
+ return $style_types{ $u->prop( $prop ) } || 'original';
}
########################################################################
--------------------------------------------------------------------------------
