Programming Ruby on Rails: active record classify constants initialize load models rake Ruby on Rails
by bseanvt
leave a comment
Load All ActiveRecord::Base Model Classes in Rails Application
Here is a simple rake task which will instantiate all of your Active Record models, provided that they are located in the RAILS_ROOT/app/models directory. Interestingly, all plugin models are instantiated by default when you run the task, for instance, if you are using the Acts As Taggable On plugin, you have access to Tag, Tagging without having to include the plugin models directory path to the task.
namespace :load_ar do
desc "load up all active record models"
task :models => :environment do
models = ActiveRecord::Base.send(:subclasses)
Dir["#{RAILS_ROOT}/app/models/*"].each do |file|
model = File.basename(file, ".*").classify
models << model unless models.include?(model)
end
end
end
If your’re in the console, you can get all the load paths for your Active Record models with the following from the API.
Rails.configuration.load_paths.each do |path| p path end
Programming Wordpress php: chris pearson gpl legality matt mullenweg mixergy php license thesis Wordpress
by bseanvt
leave a comment
Matt Mullenweg is Wrong. Thesis Should Carry Whatever License It Wants.
There is a debate underway about the legality of Thesis, a popular, paid for WordPress theme, using a different license than the one WordPress uses, which is the GPL. Matt Mullenweg, founder of WordPress, contends that all themes for the platform should inherit the WordPress license and should be considered a part of the platform. Chris Pearson, creator of Thesis, obviously has a different opinion. There have been many heated debates over the topic, but I haven’t heard an argument by either side that is compelling one way or the other.
My opinion, which I obviously think is compelling, though is not in any way rooted in any facet of the law or articulated in any legalese whatsoever, is that Matt Mullenweg is wrong. I love what Matt has done. I use and love the WordPress platform. I also think that his position is noble and rooted in the greater good for the WordPress community and open source in general.
I think what has been overlooked in his argument is an assumption we have about software, namely how it is supposed to work and what defines working software in relation to a pluggable software stack, such as WordPress. Technically, I could create an empty my-new-index.php file, place it inside the WordPress directory structure, and I have extended the platform. According to Matt, my software which does nothing, must inherit the WordPress license. Now if I want to sell (or redistribute) this do nothing piece of software for $50.00 (available upon request) I must also sell it with the GPL license. So technically, any piece of PHP code can placed inside of the WordPress directory structure and therefore, all PHP code must also carry the WordPress license!?!
There is obviously some rational limit and discretion available to us so that this sort of thing won’t ever happen! We could say because the Thesis theme makes use of the WordPress API, it must be intended for use with WordPress. However, the WordPress API is a bunch of PHP functions and WordPress itself uses the PHP API, which carries a different license (The PHP License). Which one takes precedence (if they were conflicting) ? Do all PHP applications, because they all must, by definition, use the PHP API, have to inherit the PHP License? This would be ridiculous and I’ve never seen a PHP app with the PHP license, so clearly using an API can’t include licensing requirements, so long as you aren’t modifying and redistributing the original API itself. For example, if I use a PEAR class, I don’t have to inherit the license for all of my work, I just can’t change the license of that class and or modify the original code without making it available again. Thesis doesn’t (to my knowledge) modify or extend any of the WordPress core. It provides interface enhancements, outputted HTML generated not by WordPress, but by Thesis. WordPress just consumes the Thesis software because it exists in a certain location, placed by someone into the WordPress directory structure.
This is the nature of pluggable systems, or software stacks. They are meant to be interoperable with other pieces of software whose parts and purposes are not known ahead of time. This covers a lot of space, space that isn’t even in existence and which it would, if Matt were correct, claim licensing rights to, in the event “it” comes into existence and “works” with said piece of software. Clearly Thesis “works” with WordPress. But so does a blank file. I intend for that file to do nothing, and it performs (quite efficiently) that task!
At the end of the day, it is only because Thesis is marketed to the WordPress community and is intended to be used with the WordPress platform, that there is an argument about how the software should be licensed. But Thesis is not sold bundled with WordPress, nor is anyone who purchases it required to use it with WordPress. For all intents and purposes, it is a stand alone piece of code, that well, does nothing important or worthwhile. Can you really license functionality that may or may not exist depending on how the purchaser decides to implement their purchase, just like my overpriced(though well worth the investment), blank PHP file could be used in any PHP application?
I have a lot of questions (I haven’t read all the licenses either, so I may be in total error
. Though from what I’ve read and heard so far, it is only because we have an expectation of how the software will work when it is in the WordPress directory structure that there is a conflict with how Chris has licensed Thesis. This expectation is set by the marketing of the software, not necessarily the software itself. Aside from Trademark infringement, does the language used to sell a product carry over into the the actual license the code must carry? This wouldn’t make sense. Say for instance, “Runs on Windows”, would require another license too?
I think we’re going to see a lot more arguments over licensing conflicts like what we’re seeing with WordPress/Thesis. Software is the biggest it has ever been. Open Source software is integrated into the business world like never before and business models are built on top of many of the various projects which are out there, “free” to use. As this space expands, no doubt more and more gray areas will as well.
On a practical note, what would stop Chris from writing a simple wrapper around the WordPress API his files use, license this with the GPL, use his wrapper functions instead from inside Thesis and then keep the current Thesis license?
Programming Wordpress: data export import links lynx opml xml
by bseanvt
leave a comment
How to Import/Export Your WordPress Blogroll… er, Your Links
It’s not immediately apparent how to import/export the links in your WordPress blogroll. One would expect that the import/export tool, used to backup/restore WordPress posts and pages would handle this functionality as well. But the import/export tool has many options. You need to select the Blogroll import option (located near the top of the list) and provide the tool with either the destination of a valid OPML XML url, or upload a valid OPML XML file. If you’re running on WordPress, this can be found by appending a “/wp-links-opml.php” to the URL of your domain. For example, http://seanbehan.com/wp-links-opml.php will show you my blogroll in XML. If your running out of a subdirectory, don’t forget to include that path in the URL.
If for some reason you’re running a development website on your localhost, obviously the live, remote site will not be able to contact your localhost for the data. You will have to upload the XML file to the remote site. The WordPress export tool does not provide the means to generate a the OPML XML file for you. You will have to visit the /wp-links-opml.php file and download the contents yourself, but this depends on how your browser handles XML files. Firefox will show you the raw XML, however, Safari will show you the contents of the XML nodes… which will most likely be only the name of your website. You’ll need to go to “View Source” option to see the actual XML code.Copy and past the XML to a file with the .xml file extension somewhere on your hard drive. Visit the import > blogroll tool and upload the file!
A simple way to download the file contents is to use the command line web browser Lynx. http://en.wikipedia.org/wiki/Lynx_%28web_browser%29
Just give Lynx the destination URL and the file to write to (it doesn’t have to exist) and then you can upload this file
<pre>
lynx -source URL > my-links.xml
</pre>
More info on lynx available here: http://kb.iu.edu/data/aczi.html
Programming moodle php: context course has_capbility moodle php teacher
by bseanvt
1 comment
How to Check if the Current Logged In User can Edit a Course in MOODLE Using the Has_capability Function
After an hour of fruitless searching through the docs and forums, I finally found an answer to my simple question, of course buried in the depths of the shark infested, murky water that is the Moodle code base .
How do I check if a user is a course creator for a given course? I’m still not 100% sure, the access control policy seems needlessly confusing on first glance and even after reading the heady and mostly pointless, expose on how roles/perms are calculated (Available Here: http://docs.moodle.org/en/How_permissions_are_calculated ) If you notice the section titled “Some Practical Examples” is 100% empty! (I’ve added my code to the wiki so not 100% empty any longer) The most useful part of the docs are completely empty! Instead boatloads of time went into a treatise on the internals and lovely graphs like this one 
But you could always hop into the forum dedicated just to this chunk of the code base (Available here: http://moodle.org/mod/forum/view.php?f=941&page=0) and search through the 500 topics w/ 5000+(i didn’t actually count) nested discussions (including version 1.7 of moodle as well).
No, instead, ACK came to the rescue and I found a snippet that performed the task.
Without further ado
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if (has_capability('moodle/course:update', $coursecontext)) {
/** do stuff here */
}
There it is. That could have saved me an hour if mentioned in the docs!
Linux Programming: compression exclude photoshop psd zip
by bseanvt
leave a comment
Recursively Zip Up a Directory while Excluding Certain Files Based on File Extension Type
In the example below, I’m going to zip up a directory that includes images in both PNG and PSD file formats. However, I want to exclude the PSDs because they are huge!
zip -r my-compressed-dir-without-psd.zip directory-to-zip -x '*.psd'
Linux Programming: bzip2 commands compression tar uncompress unix
by bseanvt
leave a comment
Uncompress A Bz2 File Using Tar Command
Uncompress a bz2 file using tar command
tar --use-compress-program bzip2 -xvf your-file-to-uncompress.tar.bz2
@source http://www.kde.gr.jp/help/doc/kdebase/doc/khelpcenter/faq/HTML/bzip2.html
Convert Array to Object in PHP
function array2obj($data) {
return is_array($data) ? (object) array_map(__FUNCTION__,$data) : $data;
}
Source: http://www.serversidemagazine.com/php/how-to-convert-array-notation-to-object-notation
Programming Wordpress: api children functionality get_categories parent Wordpress
by bseanvt
2 comments
Get Child Categories of Parent Category on Single Post in WordPress
I was hoping to find a function in the WordPress API, that goes something like this…
the_child_categories("Vermont");
// print Burlington, Brattleboro... etc if this post has any child categories of Vermont
But I could not. The result that I did find, from various forums goes something like this…
$parentcat = get_category_by_slug('Vermont');
foreach((get_the_category()) as $childcat):
if (cat_is_ancestor_of($parentcat, $childcat)):
echo get_category_link($childcat->cat_ID);
echo $childcat->cat_name;
endif;
endforeach;
If you’re in the in the Loop, you can use the get_the_category() function to retrieve the category objects of the current post, without printing the results to the screen. You can loop over the contents from what is returned by the function.
This condition uses the API function “cat_is_ancestor_of” to check whether or not the category from the current post is a child of the parent which we fetched with the “get_category_by_slug” function.
Here is a little function that I threw into “functions.php” so that I can get the child categories easily in my theme.
/** Usage
_the_category_children("Vermont"); */
function _the_category_children($slug=""){
if($categories = get_the_category()):
if($slug_category = get_category_by_slug($slug)):
foreach($categories as $category):
echo (cat_is_ancestor_of($slug_category, $category)) ? $category->cat_name : '';
endforeach;
endif;
endif;
}
More info/references on accomplishing this functionality are available
http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of
http://wordpress.org/support/topic/284057?replies=8#post-1120489
http://wordpress.org/support/topic/297615
Programming Wordpress: 404 admin permalinks taxonomies taxonomy Wordpress
by bseanvt
leave a comment
How To Flush Your Permalink Structure in WordPress When Using Taxonomies …or WordPress Taxonomies Not Working Instead I See A 404 Page
I didn’t know this, but because of a linking problem using custom taxonomies in WordPress, I was forced to find out. If you create a new taxonomy, it will not work immediately on the front facing end. Your users will be greeted by a 404, page not found instead. This isn’t ideal for obvious reasons.
You create and/or register custom taxonomies like so. You may have multiple taxonomies.
add_action('init', 'create_my_taxonomy', 0);
function create_my_taxonomy(){
register_taxonomy( 'musicians',
'post',
array(
'hierarchical' => false,
'label' => 'People who play lovely music that I like...',
'query_var' => 'musicians',
'rewrite' => array('slug'=>'musicians')
)
);
}
In your theme directory you need at “taxonomy.php” file. This will catch your custom taxonomy requests. You can get even more fancy and create a file like so “taxonomy-musicians.php” which you can use to differentiate your taxonomies look/feel. Nice and slick… however, you may be frustrated after following these two steps and be greeted with a friendly 404 page not found template instead.
Fear not, you need only visit the Admin >> Settings >> Permalinks page as a logged in admin to flush your rewrite rules and get linking and your correct custom taxonomy template file. Straight forward, unless if you didn’t know that merely visiting the Permalinks Settings page would correct the problem. But Alas, you do!
Addendum
You must also have posts using the taxonomy! Otherwise, you will still get the page 404 not found! I’m not sure if there is a work around for this at the moment. Anyone know?
Programming Ruby on Rails: files nil partial render rescue
by bseanvt
leave a comment
Render Partial if File Exists
If you ever want to render a partial but don’t want an error thrown you can either check for the existence of the file first
<%= render :partial => params[:controller]+"/sidebar" if File.exists?(RAILS_ROOT+"/app/views/"+params[:controller]+"/_sidebar.html.erb")%>
or you can catch the error that Rails throws
<%= render :partial => params[:controller]+"/sidebar" rescue nil %>